コード例 #1
0
        public async Task GetCurrentWeatherConditionInShouldReturnWeather()
        {
            // ARRANGE
            _configurationMock.Setup(p => p.GetSection("WeatherApiBaseUrl").Value).Returns(weatherApiUrl);
            
            var httpClient = new Mock<HttpClient>();

            var mockHttpMessageHandler = new Mock<HttpMessageHandler>();
            mockHttpMessageHandler.Protected()
                .Setup<Task<HttpResponseMessage>>("SendAsync", ItExpr.IsAny<HttpRequestMessage>(), ItExpr.IsAny<CancellationToken>())
                .ReturnsAsync(new HttpResponseMessage
                {
                    StatusCode = HttpStatusCode.OK,
                    Content = new StringContent(GetresponseContent()),
                });
            var client = new HttpClient(mockHttpMessageHandler.Object);
            _httpClientFactoryMock.Setup(_ => _.CreateClient(It.IsAny<string>())).Returns(client);

            var weatherApiClient = new WeatherApiClient(_loggerMock.Object, _httpClientFactoryMock.Object, _configurationMock.Object);

            //  ACT
            var result = await weatherApiClient.GetCurrentWeatherConditionIn("London");

            //  ASSERT
            Assert.AreEqual(name, result.Location.Name);
            Assert.AreEqual(region, result.Location.Region);
            Assert.AreEqual(country, result.Location.Country);
        }