コード例 #1
0
        public void Get_Current_Weather_Success()
        {
            var payload = new
            {
                cod   = 200,
                coord = new { lon = 9.89, lat = 53.47 }
            };

            //Arrange
            _mockHttpMessageHandler.Protected().
            Setup <Task <HttpResponseMessage> >("SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>())
            .ReturnsAsync(new HttpResponseMessage
            {
                StatusCode = HttpStatusCode.OK,
                Content    = new StringContent(JsonSerializer.Serialize(payload))
            });

            _mockHttpClient = new Mock <HttpClient>(_mockHttpMessageHandler.Object);

            _client = new OpenWeatherMapClient(_mockHttpClient.Object, _mockConfig);

            var response = _client.GetCurrentWeatherByCity(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>());

            Assert.IsNotNull(response);
            Assert.IsNotNull(response.Result);
            Assert.IsInstanceOfType(response.Result, typeof(CurrentWeatherResponse));
            Assert.AreEqual(payload.cod, response.Result.Code);
            Assert.AreEqual(payload.coord.lat, response.Result.Coordinates.Lat);
            Assert.AreEqual(payload.coord.lon, response.Result.Coordinates.Lon);
        }
コード例 #2
0
        public void Get_Current_Weather_Fail()
        {
            //Arrange
            _mockHttpMessageHandler.Protected().
            Setup <Task <HttpResponseMessage> >("SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>())
            .ReturnsAsync(new HttpResponseMessage
            {
                StatusCode = HttpStatusCode.BadRequest
            });

            _mockHttpClient = new Mock <HttpClient>(_mockHttpMessageHandler.Object);

            _client = new OpenWeatherMapClient(_mockHttpClient.Object, _mockConfig);

            var response = _client.GetCurrentWeatherByCity(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>());

            Assert.IsNull(response.Result);
        }