public async void ShouldCallCreatePostApi()
        {
            var handlerMock = new Mock <HttpMessageHandler>();
            var response    = new HttpResponseMessage
            {
                StatusCode = HttpStatusCode.OK,
                Content    = new StringContent(@"{ ""id"": 101 }"),
            };

            handlerMock
            .Protected()
            .Setup <Task <HttpResponseMessage> >(
                "SendAsync",
                ItExpr.IsAny <HttpRequestMessage>(),
                ItExpr.IsAny <CancellationToken>())
            .ReturnsAsync(response);
            var httpClient = new HttpClient(handlerMock.Object);
            var posts      = new PostClientService(httpClient);

            var retrievedPosts = await posts.CreatePost("Best post");

            handlerMock.Protected().Verify(
                "SendAsync",
                Times.Exactly(1),
                ItExpr.Is <HttpRequestMessage>(req => req.Method == HttpMethod.Post),
                ItExpr.IsAny <CancellationToken>());
        }
        public async void ShouldReturnPosts()
        {
            var handlerMock = new Mock <HttpMessageHandler>();
            var response    = new HttpResponseMessage
            {
                StatusCode = HttpStatusCode.OK,
                Content    = new StringContent(@"[{ ""id"": 1, ""title"": ""Cool post!""}, { ""id"": 100, ""title"": ""Some title""}]"),
            };

            handlerMock
            .Protected()
            .Setup <Task <HttpResponseMessage> >(
                "SendAsync",
                ItExpr.IsAny <HttpRequestMessage>(),
                ItExpr.IsAny <CancellationToken>())
            .ReturnsAsync(response);

            var httpClient = new HttpClient(handlerMock.Object);
            var posts      = new PostClientService(httpClient);

            var retrievedPosts = await posts.GetPosts();

            Assert.NotNull(retrievedPosts);
            handlerMock.Protected().Verify(
                "SendAsync",
                Times.Exactly(1),
                ItExpr.Is <HttpRequestMessage>(req => req.Method == HttpMethod.Get),
                ItExpr.IsAny <CancellationToken>());
        }
Beispiel #3
0
 public PostController(PostContext postsContext,
                       PostClientService postClientService)
 {
     _postsContext      = postsContext;
     _postClientService = postClientService;
 }