public void WeatherReaderAsync_Success() { //arrange IWeatherServiceReader accuWeatherServiceReader = new AccuWeatherServiceReader("http://accuweather:60827/Weather/london"); var httpResponseMessage = new HttpResponseMessage { Content = new WeatherResultContent(new { TemperatureCelsius = 10, WindSpeedKph = 8 }), StatusCode = HttpStatusCode.OK }; var testHttpMessageHandler = new WeatherTestHttpMessageHandler (request => { Assert.AreEqual("http://accuweather:60827/Weather/london", request.RequestUri.ToString()); Assert.AreEqual(HttpMethod.Get, request.Method); }, httpResponseMessage); HttpClient httpClient = new HttpClient(testHttpMessageHandler); // act WeatherResultInCelsiusAndKph weatherResult = null; Task.Run(async() => { weatherResult = await accuWeatherServiceReader.WeatherReaderAsync(httpClient); }).Wait(); // assert Assert.IsTrue(weatherResult.Temperature == 10 && weatherResult.WindSpeed == 8); }
public void GetWeatherResultAsync_Success() { // arrange Mock <IWeatherServiceReadersFactory> weatherServiceReadersFactory = new Mock <IWeatherServiceReadersFactory>(); Mock <IWeatherServiceReader> bbccWeatherServiceReader = new Mock <IWeatherServiceReader>(); bbccWeatherServiceReader.Setup(x => x.WeatherReaderAsync(It.IsAny <HttpClient>())).Returns(async() => { await Task.Yield(); return(new WeatherResultInCelsiusAndKph { Temperature = 20, WindSpeed = 16 }); }); Mock <IWeatherServiceReader> accuWeatherServiceReader = new Mock <IWeatherServiceReader>(); accuWeatherServiceReader.Setup(x => x.WeatherReaderAsync(It.IsAny <HttpClient>())).Returns(async() => { await Task.Yield(); return(new WeatherResultInCelsiusAndKph { Temperature = 10, WindSpeed = 8 }); }); weatherServiceReadersFactory.Setup(x => x.CreateBbcWeatherServiceReader(bbcweatherserviceUrl)).Returns(bbccWeatherServiceReader.Object); weatherServiceReadersFactory.Setup(x => x.CreateAccuWeatherServiceReader(accweatherserviceUrl)).Returns(accuWeatherServiceReader.Object); // act IWeatherServiceClient weatherServiceClient = new WeatherServiceClient(weatherServiceReadersFactory.Object); WeatherResultInCelsiusAndKph weatherResult = null; Task.Run(async() => { weatherResult = await weatherServiceClient.GetWeatherResultAsync(new HttpClient(), "london"); }).Wait(); // assert Assert.IsTrue(weatherResult.Temperature == 15 && weatherResult.WindSpeed == 12); }
public async Task <WeatherResultInCelsiusAndKph> Get(string location, string temperatureUnit, string windSpeedUnit) { TemperatureUnits inputTemperatureUnit; WindSpeedUnits inputWindSpeedUnit; if (!Enum.TryParse(temperatureUnit, out inputTemperatureUnit) || !Enum.TryParse(windSpeedUnit, out inputWindSpeedUnit)) { return(null); } WeatherResultInCelsiusAndKph weatherResultInCelsiusAndKph = await _weatherServiceClient.GetWeatherResultAsync(new HttpClient(), location); return(new WeatherResultInCelsiusAndKph { // TO DO: AutoMapper Temperature = TemperatureUnits.Celsius == inputTemperatureUnit ? weatherResultInCelsiusAndKph.Temperature : weatherResultInCelsiusAndKph.Temperature.ConvertCelsiusToFahrenheit(), WindSpeed = WindSpeedUnits.Kph == inputWindSpeedUnit ? weatherResultInCelsiusAndKph.WindSpeed : weatherResultInCelsiusAndKph.WindSpeed.ConvertKphToMph() }); }