public void GetStoryDetail_StoryIsNotInCache_SaveToCacheAndReturnStoryFromApi()
        {
            var response = "{\"by\": \"me\",\"descendants\": 1,\"id\": 1,\"kids\": [],\"score\": 1," +
                           "\"time\": 1570887781,\"title\": \"A uBlock...\",\"type\": \"story\"," +
                           "\"url\": \"http://example.com\"}";

            _cache.Setup(c => c.TryRead(1, out response))
            .Returns(false);

            _httpMessageHandler.Protected()
            .Setup <Task <HttpResponseMessage> >(
                "SendAsync",
                ItExpr.IsAny <HttpRequestMessage>(),
                ItExpr.IsAny <CancellationToken>())
            .ReturnsAsync(new HttpResponseMessage()
            {
                StatusCode = HttpStatusCode.OK,
                Content    = new StringContent(response)
            });

            var result   = _service.GetStoryDetail(1).Result;
            var shouldBe = JsonConvert.DeserializeObject <Story>(response);

            _cache.Verify(c => c.Set(1, response, It.IsAny <int>()));
            Assert.IsType <Story>(result);
            Assert.Equal(shouldBe.Score, result.Score);
            Assert.Equal(shouldBe.Time, result.Time);
            Assert.Equal(shouldBe.Title, result.Title);
            Assert.Equal(shouldBe.Uri, result.Uri);
            Assert.Equal(shouldBe.CommentCount, result.CommentCount);
            Assert.Equal(shouldBe.PostedBy, result.PostedBy);
        }
예제 #2
0
        public async Task <IActionResult> GetAll()
        {
            var result  = new List <Story>();
            var stories = await _service.GetBestStoriesIds();

            Parallel.ForEach(stories, s =>
                             result.Add(_service.GetStoryDetail(s).Result));

            return(Ok(result
                      .OrderByDescending(s => s.Score)
                      .Take(20)
                      .ToList()));
        }