Esempio n. 1
0
        private const string ItemPath      = "item/{0}.json"; // where {0} is replaced with the item id

        public HackerNewsService(ICosmosDbService cosmosDbService, HttpClient httpClient, IOptions <HackerNewsConfig> _configOptions)
        {
            _cosmosDbService        = cosmosDbService;
            _httpClient             = httpClient;
            _hackerNewsConfig       = _configOptions.Value;
            _httpClient.BaseAddress = new Uri(_hackerNewsConfig.BaseUrl);
        }
        public async Task CanGetStories(int?count, HttpResponseMessage latestStoriesResponse, IEnumerable <HackerNewsItemEntity> newsItems)
        {
            // Arrange
            var config = new HackerNewsConfig
            {
                BaseUrl             = "http://test.com",
                DefaultArticleCount = 10
            };
            var cosmosDbService = new Mock <ICosmosDbService>();

            cosmosDbService.Setup(x => x.GetHackerNewsItems(It.IsAny <Expression <Func <HackerNewsItemEntity, bool> > >())).ReturnsAsync(newsItems);
            cosmosDbService.Setup(x => x.AddHackerNewsItem(It.IsAny <HackerNewsItemEntity>()));

            var httpMessageHandler = new Mock <HttpMessageHandler>();

            httpMessageHandler.Protected()
            .Setup <Task <HttpResponseMessage> >("SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>())
            .Returns((HttpRequestMessage y, CancellationToken z) => GetResponseMessage(y, latestStoriesResponse));

            var httpClient = new HttpClient(httpMessageHandler.Object);

            var service = new HackerNewsService(cosmosDbService.Object, httpClient, Options.Create(config));

            // Act
            var response = await service.GetStories(count);

            // Assert
            Assert.IsNotNull(response);
            Assert.IsTrue(response.Count() <= count.GetValueOrDefault());
        }