Пример #1
0
        public async void SendGetRequestAndReturnTodoItemWhenGetTodosByIdAsync()
        {
            // Arrange
            _mockHandler.Protected()
            .Setup <Task <HttpResponseMessage> >("SendAsync",
                                                 ItExpr.IsAny <HttpRequestMessage>(),
                                                 ItExpr.IsAny <CancellationToken>())
            .ReturnsAsync(new HttpResponseMessage
            {
                Content = new StringContent(@"
                            {
                                'id': 1,
                                'text': 'Learn programming',
                                'isDone': false
                            }")
            });

            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.GetTodosByIdAsync(It.IsAny <int>());

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

            Assert.NotNull(result);
            Assert.IsType <TodoItemDto>(result);
        }