Esempio n. 1
0
        public void It_can_use_first_profile_in_collection_and_get_results_from_cacheProvider()
        {
            var cacheProvider   = new Mock <ICacheProvider>();
            var expectedProfile = new TestableMediatr.CacheableProfile();
            var request         = new TestableMediatr.Request {
                Id = Guid.NewGuid().ToString()
            };
            var behavior =
                new CachingPipelineBehavior <TestableMediatr.Request,
                                             TestableMediatr.Response>(
                    new List <ICachingProfile <TestableMediatr.Request,
                                               TestableMediatr.Response> >()
            {
                expectedProfile, new TestableMediatr.CacheableProfile()
            }, new Logger(),
                    cacheProvider.Object);

            var expected = new TestableMediatr.Response();
            Func <Task <TestableMediatr.Response> > requestHandlerDelegate = async() => expected;

            cacheProvider.Setup(a => a.GetAsync <TestableMediatr.Response>(
                                    expectedProfile.GetCacheKey(request),
                                    It.IsAny <DateTime>(),
                                    It.IsAny <Func <Task <TestableMediatr.Response> > >()))
            .ReturnsAsync(expected);

            var result = behavior.Handle(request,
                                         new CancellationToken(), async() => expected).Result;

            result.ShouldBe(expected);
        }
        public void It_can_throw_exception_when_cacheProvider_throws_exception()
        {
            var request         = new TestableMediatr.Request();
            var expectedProfile = new TestableMediatr.CacheableProfile();

            TestableMediatr.Response expected = new TestableMediatr.Response {
                Id = "Randy"
            };
            _mockCacheProvider.Setup(a => a.GetAsync <TestableMediatr.Response>(
                                         expectedProfile.GetCacheKey(request),
                                         It.IsAny <DateTime>(),
                                         It.IsAny <Func <Task <TestableMediatr.Response> > >()))
            .Throws <ArgumentOutOfRangeException>();
            Should.Throw <ArgumentOutOfRangeException>(async() => await _mediator.Send(new TestableMediatr.Request()));
        }
        public void It_can_return_result_from_cache_provider()
        {
            var request         = new TestableMediatr.Request();
            var expectedProfile = new TestableMediatr.CacheableProfile();

            TestableMediatr.Response expected = new TestableMediatr.Response {
                Id = "Randy"
            };
            _mockCacheProvider.Setup(a => a.GetAsync <TestableMediatr.Response>(
                                         expectedProfile.GetCacheKey(request),
                                         It.IsAny <DateTime>(),
                                         It.IsAny <Func <Task <TestableMediatr.Response> > >()))
            .ReturnsAsync(expected);
            var result = _mediator.Send(new TestableMediatr.Request()).Result;

            result.Id.ShouldBe(expected.Id);
        }