Example #1
0
        public void Background()
        {
            _mockHttpMessageHandler = new MockHttpMessageHandler();
            var locationBuilder = new TestLocationResponseBuilder();
            var weatherBuilder  = new TestWeatherResponseBuilder();

            _mockHttpMessageHandler.When(STR_Search)
            .WithQueryString("query", "Belfast")
            .Respond(() => locationBuilder.Default().WithBelfast().BuildHttpResponse());

            _mockHttpMessageHandler.When(STR_Search)
            .WithQueryString("query", "Birmingham")
            .Respond(() => locationBuilder.Default().WithBirmingham().BuildHttpResponse());

            _mockHttpMessageHandler.When(STR_44544)
            .Respond(() => weatherBuilder.Default().WithBelfast().BuildHttpResponse());

            _mockHttpMessageHandler.When(STR_12723)
            .Respond(() => weatherBuilder.Default().WithBirmingham().BuildHttpResponse());

            _mockHttpMessageHandler.Fallback
            .Respond(
                req => new HttpResponseMessage(HttpStatusCode.NotFound)
            {
                ReasonPhrase =
                    $"No matching mock handler found for \"{req.Method.ToString().ToUpperInvariant()} {req.RequestUri.AbsoluteUri}\""
            });

            var settings = new RefitSettings {
                HttpMessageHandlerFactory = () => _mockHttpMessageHandler
            };

            _metaWeatherService = RestService.For <IMetaWeatherService>("https://www.metaweather.com", settings);
        }
        public async Task WeatherController_GetWithValidWoeId_ReturnsValidWeatherResponse(ILogger <WeatherController> logger,
                                                                                          ApiOptions apiOptions,
                                                                                          IApiProxy apiProxy)
        {
            var expectedCount = 5;

            _apiProxy = apiProxy;

            // Arrange
            _apiProxy = Substitute.For <IApiProxy>();
            _weatherResponseBuiler = new TestWeatherResponseBuilder();

            _weatherController = new WeatherController(logger, apiOptions, _apiProxy);
            _apiProxy.SubmitWeatherRequest(Arg.Any <IWeatherRequest>())
            .Returns(_weatherResponseBuiler.Default().WithBelfast().Build());

            // Act
            var weatherResponse = await _weatherController.Post(new WeatherRequest()).ConfigureAwait(false);

            // Assert
            using (new AssertionScope())
            {
                weatherResponse.Should().BeOkObjectResult().ValueAs <IWeatherResponse>().Forecasts
                .Should()
                .HaveCount(expectedCount);
            }
        }