Exemplo n.º 1
0
        public async Task Returns_A_WeatherForecast()
        {
            var opts          = OptionsBuilder.OpenWeatherConfig();
            var clientFactory = ClientBuilder.OpenWeatherClientFactory(OpenWeatherResponses.OkResponse);
            var sut           = new OpenWeatherService(opts, clientFactory);

            var result = await sut.GetFiveDayForecastAsync("Chicago");

            Assert.IsType <List <WeatherForecast> >(result);
        }
Exemplo n.º 2
0
        public async Task Returns_OpenWeatherException_On_OpenWeatherInternalError()
        {
            var opts          = OptionsBuilder.OpenWeatherConfig();
            var clientFactory = ClientBuilder.OpenWeatherClientFactory(OpenWeatherResponses.InternalErrorResponse,
                                                                       HttpStatusCode.InternalServerError);
            var sut = new OpenWeatherService(opts, clientFactory);

            var result = await Assert.ThrowsAsync <OpenWeatherException>(() => sut.GetFiveDayForecastAsync("New York"));

            Assert.Equal(500, (int)result.StatusCode);
        }
Exemplo n.º 3
0
        public async Task Returns_OpenWeatherException_When_Unauthorized()
        {
            var opts          = OptionsBuilder.OpenWeatherConfig();
            var clientFactory = ClientBuilder.OpenWeatherClientFactory(OpenWeatherResponses.UnauthorizedResponse,
                                                                       HttpStatusCode.Unauthorized);
            var sut = new OpenWeatherService(opts, clientFactory);

            var result = await Assert.ThrowsAsync <OpenWeatherException>(() => sut.GetFiveDayForecastAsync("Chicago"));

            Assert.Equal(401, (int)result.StatusCode);
        }
Exemplo n.º 4
0
        public async Task Returns_OpenWeatherException_When_Called_With_Bad_Argument()
        {
            var opts          = OptionsBuilder.OpenWeatherConfig();
            var clientFactory = ClientBuilder.OpenWeatherClientFactory(OpenWeatherResponses.NotFoundResponse,
                                                                       HttpStatusCode.NotFound);
            var sut = new OpenWeatherService(opts, clientFactory);

            var result = await Assert.ThrowsAsync <OpenWeatherException>(() => sut.GetFiveDayForecastAsync("Westeros"));

            Assert.Equal(404, (int)result.StatusCode);
        }
Exemplo n.º 5
0
        public async Task Returns_Expected_Values_From_the_Api()
        {
            var opts          = OptionsBuilder.OpenWeatherConfig();
            var clientFactory = ClientBuilder.OpenWeatherClientFactory(OpenWeatherResponses.OkResponse);
            var sut           = new OpenWeatherService(opts, clientFactory);

            var result = await sut.GetFiveDayForecastAsync("Chicago");

            Assert.Equal(new DateTime(1594155600), result[0].Date);
            Assert.Equal((decimal)32.93, result[0].Temp);
        }
Exemplo n.º 6
0
        public async Task return_a_weatherforecast()
        {
            var handler = new HttpMessageHandlerMock(OpenWeatherResponses.OkResponse);

            var client        = new HttpClient(handler);
            var clientFactory = Substitute.For <IHttpClientFactory>();

            clientFactory.CreateClient().Returns <HttpClient>(client);

            var sut = new OpenWeatherService(_openWeatherConfiguration, clientFactory);

            var weatherForecasts = await sut.GetFiveDayForecastAsync("Chicago");

            weatherForecasts.Should().BeOfType <List <WeatherForecast> >();
        }
Exemplo n.º 7
0
        public async Task Returns_expected_forecasts_from_the_api()
        {
            var handler = new HttpMessageHandlerMock(OpenWeatherResponses.OkResponse);

            var client        = new HttpClient(handler);
            var clientFactory = Substitute.For <IHttpClientFactory>();

            clientFactory.CreateClient().Returns <HttpClient>(client);

            var sut = new OpenWeatherService(_openWeatherConfiguration, clientFactory);

            var weatherForecastResponse = await sut.GetFiveDayForecastAsync("Chicago");

            weatherForecastResponse[0].Date.Should().Be(new DateTime(1594155600));
            weatherForecastResponse[0].Temp.Should().BeApproximately((decimal)32.93, 2);
            //Assert.Equal(new DateTime(1594155600), result[0].Date);
            //assert.equal((decimal)32.93, result[0].temp);
        }
Exemplo n.º 8
0
        public void Return_openweatherexception_when_unauthorized()
        {
            var handler = new HttpMessageHandlerMock(OpenWeatherResponses.UnauthorizedResponse,
                                                     HttpStatusCode.Unauthorized);

            var client        = new HttpClient(handler);
            var clientFactory = Substitute.For <IHttpClientFactory>();

            clientFactory.CreateClient().Returns <HttpClient>(client);


            var sut = new OpenWeatherService(_openWeatherConfiguration, clientFactory);

            var weatherForecastResponse = Assert.ThrowsAsync <OpenWeatherException>(async() => await sut.GetFiveDayForecastAsync("Chicago"));

            weatherForecastResponse.StatusCode.Should().Be(401);
        }
Exemplo n.º 9
0
        public void Return_openweatherexception_on_openweatherinternalerror()
        {
            var handler = new HttpMessageHandlerMock(OpenWeatherResponses.InternalErrorResponse,
                                                     HttpStatusCode.InternalServerError);

            var client        = new HttpClient(handler);
            var clientFactory = Substitute.For <IHttpClientFactory>();

            clientFactory.CreateClient().Returns <HttpClient>(client);



            var sut = new OpenWeatherService(_openWeatherConfiguration, clientFactory);

            var weatherForecastResponse = Assert.ThrowsAsync <OpenWeatherException>(async() => await sut.GetFiveDayForecastAsync("New York"));

            weatherForecastResponse.StatusCode.Should().Be(500);
        }