コード例 #1
0
        public void SetUp()
        {
            _endPointMock = new Mock <ICachedStoriesEndPointDecorator>();
            _endPointMock.Setup(e => e.GetBestStoriesAsync(CancellationToken.None))
            .ReturnsAsync(() => StoriesTestHelper.GenerateStories(200).ToList());

            _loggerMock        = new Mock <ILogger <StoriesController> >();
            _storiesController = new StoriesController(_endPointMock.Object, _loggerMock.Object);
        }
コード例 #2
0
        public async Task UseCachingService()
        {
            var stories = StoriesTestHelper.GenerateStories(200).ToList();

            _storiesEndPointMock.Setup(e => e.GetBestStoriesAsync(It.IsAny <CancellationToken>()))
            .ReturnsAsync(() => stories);

            var result = await _cachedEndPoint.GetBestStoriesAsync(CancellationToken.None);

            // Expecting a call to StoriesEndPoint to get data
            _storiesEndPointMock.Verify(s => s.GetBestStoriesAsync(It.IsAny <CancellationToken>()), Times.Once);

            Assert.AreEqual(200, result.Count);

            _storiesCachingService.Setup(c => c.Get()).Returns(() => stories);

            result = await _cachedEndPoint.GetBestStoriesAsync(CancellationToken.None);

            // Verifying that after 2 calls the underlying service has only been called once and instead the data must have been retrieved from the cache
            _storiesEndPointMock.Verify(s => s.GetBestStoriesAsync(It.IsAny <CancellationToken>()), Times.Once);

            Assert.AreEqual(200, result.Count);
        }