Пример #1
0
        public async void GetCurrentWeatherAsync_Successful()
        {
            string         response           = "{\"lat\":50.96,\"lon\":7.99,\"timezone\":\"Europe/Berlin\",\"timezone_offset\":7200,\"current\":{\"dt\":1599311488,\"sunrise\":1599281304,\"sunset\":1599329117,\"temp\":15.22,\"feels_like\":13.36,\"pressure\":1018,\"humidity\":76,\"dew_point\":11.02,\"uvi\":4.43,\"clouds\":98,\"visibility\":10000,\"wind_speed\":3.13,\"wind_deg\":248,\"wind_gust\":7.15,\"weather\":[{\"id\":804,\"main\":\"Clouds\",\"description\":\"Bedeckt\",\"icon\":\"04d\"}]}}";
            HttpStatusCode statusCode         = HttpStatusCode.OK;
            var            messageHandlerMock = new HttpMessageHandlerMock(response, statusCode);
            var            httpClient         = new HttpClient(messageHandlerMock);
            var            clientFactory      = Substitute.For <IHttpClientFactory>();

            clientFactory.CreateClient().Returns(httpClient);
            var optionsMonitor = Substitute.For <IOptionsMonitor <OWMOptions> >();
            var owmOptions     = new OWMOptions()
            {
                Latitude     = 30.0f,
                Longitude    = 10.0f,
                LanguageCode = "de",
                ApiKey       = "xyz123"
            };

            optionsMonitor.CurrentValue.Returns(owmOptions);
            var dateTimeProvider = Substitute.For <IDateTimeProvider>();

            dateTimeProvider.UtcNow.Returns(new DateTime(2020, 07, 22));
            var owmProvider = new OWMProvider(clientFactory, optionsMonitor, dateTimeProvider);

            var result = await owmProvider.GetCurrentWeatherAsync();

            Assert.NotNull(result);
        }
Пример #2
0
        public async void GetCurrentWeatherAsync_InvalidJson()
        {
            string         response           = "{abc}";
            HttpStatusCode statusCode         = HttpStatusCode.OK;
            var            messageHandlerMock = new HttpMessageHandlerMock(response, statusCode);
            var            httpClient         = new HttpClient(messageHandlerMock);
            var            clientFactory      = Substitute.For <IHttpClientFactory>();

            clientFactory.CreateClient().Returns(httpClient);
            var optionsMonitor = Substitute.For <IOptionsMonitor <OWMOptions> >();
            var owmOptions     = new OWMOptions()
            {
                Latitude     = 20.1f,
                Longitude    = 10.07f,
                LanguageCode = "de",
                ApiKey       = "xyz123"
            };

            optionsMonitor.CurrentValue.Returns(owmOptions);
            var dateTimeProvider = Substitute.For <IDateTimeProvider>();

            dateTimeProvider.UtcNow.Returns(new DateTime(2020, 07, 22));
            var owmProvider = new OWMProvider(clientFactory, optionsMonitor, dateTimeProvider);

            var result = await owmProvider.GetCurrentWeatherAsync();

            Assert.Null(result);
        }