Beispiel #1
0
        public async void SendPostRequestAndReturnOkWhenAddTodoAsync()
        {
            // Arrange
            _mockHandler.Protected()
            .Setup <Task <HttpResponseMessage> >("SendAsync",
                                                 ItExpr.IsAny <HttpRequestMessage>(),
                                                 ItExpr.IsAny <CancellationToken>())
            .ReturnsAsync(new HttpResponseMessage(HttpStatusCode.OK));

            var client = new HttpClient(_mockHandler.Object);

            client.BaseAddress = new Uri("https://localhost:44376");

            _mockClientFactory.Setup(x => x.CreateClient("TodoAppClient")).Returns(client);

            var sut = new TodoItemService(_mockClientFactory.Object);

            // Act
            var result = await sut.AddTodoAsync(It.IsAny <TodoItemForCreationDto>());

            // Assert
            _mockHandler.Protected().Verify(
                "SendAsync",
                Times.Exactly(1),
                ItExpr.Is <HttpRequestMessage>(req => req.Method == HttpMethod.Post),
                ItExpr.IsAny <CancellationToken>());

            Assert.Equal(HttpStatusCode.OK, result.StatusCode);
        }