private async Task <Post> GetPost(int i) { var uri = uriProvider.GetItemUri(i); var result = await GetResponse(uri, "items"); var hackernewsPost = JsonConvert.DeserializeObject <HackernewsPost>(result); return(mapper.ToPost(hackernewsPost)); }
public void ShouldReturnPostsFromJson() { var posts = GetPosts(); var ids = new List <int> { 1, 2, 3 }; var fakeHttpMessageHandler = new FakeItemsHttpMessageHandler(posts.ToList()); var fakeHttpMessageHandler2 = new FakeHttpMessageHandler(new HttpResponseMessage() { StatusCode = HttpStatusCode.OK, Content = new StringContent(JsonConvert.SerializeObject(ids), Encoding.UTF8, "application/json") }); var fakeHttpClient = new HttpClient(fakeHttpMessageHandler); var fakeHttpClient2 = new HttpClient(fakeHttpMessageHandler2); httpClientFactory.CreateClient(Arg.Is("items")).Returns(fakeHttpClient); httpClientFactory.CreateClient(Arg.Is("topPosts")).Returns(fakeHttpClient2); uriProvider.GetTopPostsUri().Returns(new Uri("http://someuri.com")); uriProvider.GetItemUri(Arg.Any <int>()).Returns((i) => new Uri("http://someuri.com")); var hackernewsClient = new HackernewsClient(httpClientFactory, uriProvider, new MApper()); var actualPosts = hackernewsClient.GetTopPosts(posts.Count()).Result; Assert.Equal(posts.Count(), actualPosts.Count()); foreach (var post in actualPosts) { var testPost = posts.First(p => p.By == post.Author); Assert.Equal(testPost.Title, post.Title); Assert.Equal(testPost.Url.ToString(), post.Uri.ToString()); Assert.Equal(testPost.Kids.Count(), post.Comments); Assert.Equal(testPost.Score, post.Points); } }