From b573af44a749ebd3f08ac36396299f2745c170ba Mon Sep 17 00:00:00 2001 From: Kevin Logan Date: Tue, 29 Jan 2019 15:56:40 -0600 Subject: [PATCH 01/12] start to commenting things out --- source/api.tests/Weather/ApixuClientTests.cs | 97 ++++--- .../MockHttpClientHandlerExtensions.cs | 24 +- .../Weather/WeatherControllerTests.cs | 263 ++++++++++-------- .../Weather/ApiuxWeatherForecastResponse.cs | 2 - source/api/Weather/WeatherController.cs | 47 ++-- 5 files changed, 221 insertions(+), 212 deletions(-) diff --git a/source/api.tests/Weather/ApixuClientTests.cs b/source/api.tests/Weather/ApixuClientTests.cs index 35fe379..6a8f0ab 100644 --- a/source/api.tests/Weather/ApixuClientTests.cs +++ b/source/api.tests/Weather/ApixuClientTests.cs @@ -1,9 +1,7 @@ using Api.Weather; using Microsoft.VisualStudio.TestTools.UnitTesting; using Moq; -using Moq.Protected; using RichardSzalay.MockHttp; -using System; using System.Net.Http; using System.Threading.Tasks; @@ -15,64 +13,65 @@ public class ApixuClientTests { private (ApixuClient apiuxClient, MockHttpMessageHandler mockHttp) Factory() { - var fakeHttpClient = new Mock(); - - // using https://github.com/richardszalay/mockhttp + // using https://github.com/richardszalay/mockhttp not var fakeHttpClient = new Mock(); var mockHttp = new MockHttpMessageHandler(); var httpClient = mockHttp.ToHttpClient(); var apiuxClient = new ApixuClient(httpClient); return (apiuxClient, mockHttp); } - [TestMethod] - public async Task ApixuClientTests_GetTemp_GivenAZipCode_ReturnsTemp() - { - // Arrange - var (apiuxClient, mockHttp) = Factory(); - const int zipCode = 57105; - const double fakeTemp = 70.5; - var response = new ApiuxWeatherCurrentResponse - { - Current = new ApiuxWeatherCurrent - { - TempF = fakeTemp - } - }; - var requestUri = $"https://api.apixu.com/v1/current.json?key={ApixuClient.apiKey}&q={zipCode}"; - mockHttp.When(requestUri) - .Respond("application/json", Serialize.ToJson(response)); - // Act - var result = await apiuxClient.GetCurrentTempAsync(zipCode); - // Assert - Assert.AreEqual(fakeTemp, result); - } - [TestMethod] - public async Task ApixuClientTests_GetTemp_GivenAZipCode_UsesThatZipCode() - { - // Arrange - var (apiuxClient, mockHttp) = Factory(); - const int zipCode = 57105; - const double fakeTemp = 70.5; - var response = new ApiuxWeatherCurrentResponse - { - Current = new ApiuxWeatherCurrent - { - TempF = fakeTemp - } - }; - var requestUri = $"https://api.apixu.com/v1/current.json?key={ApixuClient.apiKey}&q={zipCode}"; - var request = mockHttp.When(requestUri) - .Respond("application/json", Serialize.ToJson(response)); + //[TestMethod] + //public async Task ApixuClientTests_GetTemp_GivenAZipCode_ReturnsTemp() + //{ + // // Arrange + // var (apiuxClient, mockHttp) = Factory(); + // const int zipCode = 57105; + // const double fakeTemp = 70.5; + // var response = new ApiuxWeatherCurrentResponse + // { + // Current = new ApiuxWeatherCurrent + // { + // TempF = fakeTemp + // } + // }; + // var requestUri = $"https://api.apixu.com/v1/current.json?key={ApixuClient.apiKey}&q={zipCode}"; + // mockHttp.When(requestUri) + // .Respond("application/json", Serialize.ToJson(response)); + + // // Act + // var result = await apiuxClient.GetCurrentTempAsync(zipCode); + + // // Assert + // Assert.AreEqual(fakeTemp, result); + //} + + //[TestMethod] + //public async Task ApixuClientTests_GetTemp_GivenAZipCode_UsesThatZipCode() + //{ + // // Arrange + // var (apiuxClient, mockHttp) = Factory(); + // const int zipCode = 57105; + // const double fakeTemp = 70.5; + // var response = new ApiuxWeatherCurrentResponse + // { + // Current = new ApiuxWeatherCurrent + // { + // TempF = fakeTemp + // } + // }; + // var requestUri = $"https://api.apixu.com/v1/current.json?key={ApixuClient.apiKey}&q={zipCode}"; + // var request = mockHttp.When(requestUri) + // .Respond("application/json", Serialize.ToJson(response)); - // Act - var result = await apiuxClient.GetCurrentTempAsync(zipCode); + // // Act + // var result = await apiuxClient.GetCurrentTempAsync(zipCode); - // Assert - Assert.AreEqual(1, mockHttp.GetMatchCount(request)); - } + // // Assert + // Assert.AreEqual(1, mockHttp.GetMatchCount(request)); + //} } } diff --git a/source/api.tests/Weather/MockHttpClientHandlerExtensions.cs b/source/api.tests/Weather/MockHttpClientHandlerExtensions.cs index 0dd46e4..bebdd05 100644 --- a/source/api.tests/Weather/MockHttpClientHandlerExtensions.cs +++ b/source/api.tests/Weather/MockHttpClientHandlerExtensions.cs @@ -1,12 +1,4 @@ -using Moq; -using Moq.Protected; -using System; -using System.Net; -using System.Net.Http; -using System.Threading; -using System.Threading.Tasks; - -namespace Api.Tests.Weather +namespace Api.Tests.Weather { // use https://github.com/richardszalay/mockhttp instead /// @@ -16,13 +8,13 @@ namespace Api.Tests.Weather //{ // public static void SetupGetStringAsync(this Mock mockHandler, string response, Uri requestUri) // { - // if you have to be specific or multiple calls in the same method change or want to verify it - //mockHandler.Protected() - // .Setup>("SendAsync", - // ItExpr.Is(message => message.RequestUri == requestUri), - // ItExpr.IsAny()) - // .Returns(Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(response) })) - // .Verifiable(); + // if you have to be specific or multiple calls in the same method change or want to verify it + //mockHandler.Protected() + // .Setup>("SendAsync", + // ItExpr.Is(message => message.RequestUri == requestUri), + // ItExpr.IsAny()) + // .Returns(Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(response) })) + // .Verifiable(); // } //} } diff --git a/source/api.tests/Weather/WeatherControllerTests.cs b/source/api.tests/Weather/WeatherControllerTests.cs index 1b41ca1..ffc9163 100644 --- a/source/api.tests/Weather/WeatherControllerTests.cs +++ b/source/api.tests/Weather/WeatherControllerTests.cs @@ -1,10 +1,6 @@ using Api.Weather; -using Microsoft.AspNetCore.Mvc; using Microsoft.VisualStudio.TestTools.UnitTesting; using Moq; -using System; -using System.Collections.Generic; -using System.Threading.Tasks; namespace Api.Tests.Weather { @@ -18,137 +14,160 @@ public class WeatherControllerTests return (new WeatherController(getWeatherHttpClient.Object), getWeatherHttpClient); } - [TestMethod] - public async Task WeatherController_GetCurrentTemp_NoZipCode_Returns400() - { - // Arrange - var (weatherController, getWeatherHttpClient) = Factory(); - // Act - var result = await weatherController.CurrentTemp(0); - // Assert - Assert.AreEqual(400, (result.Result as BadRequestObjectResult).StatusCode); - } - [TestMethod] - public async Task WeatherController_GetCurrentTemp_ZipCode_CallsWithZipCode() - { - /** - * Given an API call - * When asking for current temp - * Then it calls the weather Api with the correct zip code - */ - // Arrange - var zipCode = 57105; - var (weatherController, getWeatherHttpClient) = Factory(); - - // fake the return from weather provider with MOQ - var fakeTemp = 72.6; - getWeatherHttpClient.Setup(wp => wp.GetCurrentTempAsync(zipCode)) - .ReturnsAsync(fakeTemp); - - // Act - var response = await weatherController.CurrentTemp(zipCode); - - // Assert - getWeatherHttpClient.Verify(w => w.GetCurrentTempAsync(zipCode), Times.Once); - - // Note this test may not be the most useful, but it helps move us forward with TDD - // some may just delete this test or skip writting it - } - [TestMethod] - public async Task WeatherController_GetTemp_NoZipCode_Returns400() - { - // Arrange - var (weatherController, getWeatherHttpClient) = Factory(); - // Act - var result = await weatherController.CurrentTemp(0); - // Assert - Assert.AreEqual(400, (result.Result as BadRequestObjectResult).StatusCode); - } - [TestMethod] - public async Task WeatherController_GetPastTemp_NoZipCode_Returns400() - { - // Arrange - var (weatherController, getWeatherHttpClient) = Factory(); - // Act - var result = await weatherController.PastWeather(0, string.Empty); - // Assert - Assert.AreEqual(400, (result.Result as BadRequestObjectResult).StatusCode); - } - [TestMethod] - [DataRow("")] - [DataRow("00-1-2 10:00:00")] - public async Task WeatherController_GetPastTemp_NoDateTimeOrInvalid_Returns400(string dateTime) - { - // Arrange - var (weatherController, getWeatherHttpClient) = Factory(); - // Act - var result = await weatherController.PastWeather(59785, string.Empty); - // Assert - Assert.AreEqual(400, (result.Result as BadRequestObjectResult).StatusCode); - } - [TestMethod] - public async Task WeatherController_GetPastTemp_ZipCodeAndDate_CallsProviderWithZipCodeAndDate() - { - /** - * Given an API call - * When asking for past temp - * Then it calls the weather Api with the correct zip code and date time - */ - // Arrange - var zipCode = 57105; - var date = DateTime.Now; - var (weatherController, getWeatherHttpClient) = Factory(); - - // fake the return from weather provider with MOQ - var fakeTemp = 72.6; - var fakeResponse = new ApiuxWeatherForecastResponse - { - Forecast = new ApiuxWeatherForecast - { - ForecastDay = new List { - new ForecastDay - { - Hour = new List { - new ForecastHour - { - FeelslikeF = fakeTemp - } - } - } - } - } - }; - getWeatherHttpClient.Setup(wp => wp.GetPastWeatherAsync(It.IsAny(), It.IsAny())) - //It.Is(d => d.ToString() == date.ToString()))) - .ReturnsAsync(fakeResponse); - - // Act - var result = await weatherController.PastWeather(zipCode, date.ToString()); - - // Assert - getWeatherHttpClient.Verify(w => w.GetPastWeatherAsync(zipCode, - // needs to use It.Is straight date doesn't match - It.Is(d => d.ToString() == date.ToString())), - Times.Once); - - // this really only tests the MOQ - //var weatherResult = ApiuxWeatherCurrentResponse.FromJson((result.Result as OkObjectResult).Value as string); - //Assert.AreEqual(fakeResponse.Current.TempF, weatherResult.Current.TempF); - } - // TODO add more tests and code if formatting the data response to abstract from Apiux is required + + + + + + + + + + //[TestMethod] + //public async Task WeatherController_GetCurrentTemp_NoZipCode_Returns400() + //{ + // // Arrange + // var (weatherController, getWeatherHttpClient) = Factory(); + + // // Act + // var result = await weatherController.CurrentTemp(0); + + // // Assert + // Assert.AreEqual(400, (result.Result as BadRequestObjectResult).StatusCode); + //} + + //[TestMethod] + //public async Task WeatherController_GetCurrentTemp_ZipCode_CallsWithZipCode() + //{ + // /** + // * Given an API call + // * When asking for current temp + // * Then it calls the weather Api with the correct zip code + // */ + // // Arrange + // var zipCode = 57105; + // var (weatherController, getWeatherHttpClient) = Factory(); + + // // fake the return from weather provider with MOQ + // var fakeTemp = 72.6; + // getWeatherHttpClient.Setup(wp => wp.GetCurrentTempAsync(zipCode)) + // .ReturnsAsync(fakeTemp); + + // // Act + // var response = await weatherController.CurrentTemp(zipCode); + + // // Assert + // getWeatherHttpClient.Verify(w => w.GetCurrentTempAsync(zipCode), Times.Once); + + // // Note this test may not be the most useful, but it helps move us forward with TDD + // // some may just delete this test or skip writting it + //} + + //[TestMethod] + //public async Task WeatherController_GetTemp_NoZipCode_Returns400() + //{ + // // Arrange + // var (weatherController, getWeatherHttpClient) = Factory(); + + // // Act + // var result = await weatherController.CurrentTemp(0); + + // // Assert + // Assert.AreEqual(400, (result.Result as BadRequestObjectResult).StatusCode); + //} + + //[TestMethod] + //public async Task WeatherController_GetPastTemp_NoZipCode_Returns400() + //{ + // // Arrange + // var (weatherController, getWeatherHttpClient) = Factory(); + + // // Act + // var result = await weatherController.PastWeather(0, string.Empty); + + // // Assert + // Assert.AreEqual(400, (result.Result as BadRequestObjectResult).StatusCode); + //} + + //[TestMethod] + //[DataRow("")] + //[DataRow("00-1-2 10:00:00")] + //public async Task WeatherController_GetPastTemp_NoDateTimeOrInvalid_Returns400(string dateTime) + //{ + // // Arrange + // var (weatherController, getWeatherHttpClient) = Factory(); + + // // Act + // var result = await weatherController.PastWeather(59785, string.Empty); + + // // Assert + // Assert.AreEqual(400, (result.Result as BadRequestObjectResult).StatusCode); + //} + + //[TestMethod] + //public async Task WeatherController_GetPastTemp_ZipCodeAndDate_CallsProviderWithZipCodeAndDate() + //{ + // /** + // * Given an API call + // * When asking for past temp + // * Then it calls the weather Api with the correct zip code and date time + // */ + // // Arrange + // var zipCode = 57105; + // var date = DateTime.Now; + // var (weatherController, getWeatherHttpClient) = Factory(); + + // // fake the return from weather provider with MOQ + // var fakeTemp = 72.6; + // var fakeResponse = new ApiuxWeatherForecastResponse + // { + // Forecast = new ApiuxWeatherForecast + // { + // ForecastDay = new List { + // new ForecastDay + // { + // Hour = new List { + // new ForecastHour + // { + // FeelslikeF = fakeTemp + // } + // } + // } + // } + // } + // }; + // getWeatherHttpClient.Setup(wp => wp.GetPastWeatherAsync(It.IsAny(), It.IsAny())) + // //It.Is(d => d.ToString() == date.ToString()))) + // .ReturnsAsync(fakeResponse); + + // // Act + // var result = await weatherController.PastWeather(zipCode, date.ToString()); + + // // Assert + // getWeatherHttpClient.Verify(w => w.GetPastWeatherAsync(zipCode, + // // needs to use It.Is straight date doesn't match + // It.Is(d => d.ToString() == date.ToString())), + // Times.Once); + + // // this really only tests the MOQ + // //var weatherResult = ApiuxWeatherCurrentResponse.FromJson((result.Result as OkObjectResult).Value as string); + // //Assert.AreEqual(fakeResponse.Current.TempF, weatherResult.Current.TempF); + //} + + //// TODO add more tests and code if formatting the data response to abstract from Apiux is required } } diff --git a/source/api/Weather/ApiuxWeatherForecastResponse.cs b/source/api/Weather/ApiuxWeatherForecastResponse.cs index 12d681d..890b14d 100644 --- a/source/api/Weather/ApiuxWeatherForecastResponse.cs +++ b/source/api/Weather/ApiuxWeatherForecastResponse.cs @@ -1,8 +1,6 @@ using Newtonsoft.Json; using System; using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; namespace Api.Weather { diff --git a/source/api/Weather/WeatherController.cs b/source/api/Weather/WeatherController.cs index ec888fa..3895991 100644 --- a/source/api/Weather/WeatherController.cs +++ b/source/api/Weather/WeatherController.cs @@ -17,31 +17,32 @@ public WeatherController(IGetWeatherHttpClient weatherHttpClient) [HttpGet("[action]")] public async Task> CurrentTemp([FromQuery(Name = "zipcode")] int zipCode) { - if (zipCode == 0) - { - return BadRequest($"{nameof(zipCode)} cannot be 0"); - } - - var result = await weatherHttpClient.GetCurrentTempAsync(zipCode); - return Ok(result); + return Ok(zipCode); + //if (zipCode == 0) + //{ + // return BadRequest($"{nameof(zipCode)} cannot be 0"); + //} + + //var result = await weatherHttpClient.GetCurrentTempAsync(zipCode); + //return Ok(result); } - [HttpGet("[action]")] - public async Task> PastWeather([FromQuery(Name = "zipcode")] int zipCode, [FromQuery(Name = "dateTime")] string dateTime) - { - if(zipCode == 0) - { - return BadRequest($"{nameof(zipCode)} cannot be 0"); - } - - if (string.IsNullOrWhiteSpace(dateTime) || !DateTime.TryParse(dateTime, out var parsedDateTime)) - { - return BadRequest($"{nameof(dateTime)} must be a valid date"); - } - - var weather = await weatherHttpClient.GetPastWeatherAsync(zipCode, parsedDateTime); - return weather; - } + //[HttpGet("[action]")] + //public async Task> PastWeather([FromQuery(Name = "zipcode")] int zipCode, [FromQuery(Name = "dateTime")] string dateTime) + //{ + // if(zipCode == 0) + // { + // return BadRequest($"{nameof(zipCode)} cannot be 0"); + // } + + // if (string.IsNullOrWhiteSpace(dateTime) || !DateTime.TryParse(dateTime, out var parsedDateTime)) + // { + // return BadRequest($"{nameof(dateTime)} must be a valid date"); + // } + + // var weather = await weatherHttpClient.GetPastWeatherAsync(zipCode, parsedDateTime); + // return weather; + //} } } From f1bd12d4fcb39067a8adb07851acb92ea4261a1d Mon Sep 17 00:00:00 2001 From: Kevin Logan Date: Tue, 29 Jan 2019 16:26:56 -0600 Subject: [PATCH 02/12] ready for start --- .../Weather/WeatherControllerTests.cs | 271 +++++++++--------- source/api/Weather/WeatherController.cs | 9 +- 2 files changed, 140 insertions(+), 140 deletions(-) diff --git a/source/api.tests/Weather/WeatherControllerTests.cs b/source/api.tests/Weather/WeatherControllerTests.cs index ffc9163..eafdc80 100644 --- a/source/api.tests/Weather/WeatherControllerTests.cs +++ b/source/api.tests/Weather/WeatherControllerTests.cs @@ -1,6 +1,8 @@ using Api.Weather; +using Microsoft.AspNetCore.Mvc; using Microsoft.VisualStudio.TestTools.UnitTesting; using Moq; +using System.Threading.Tasks; namespace Api.Tests.Weather { @@ -14,160 +16,157 @@ public class WeatherControllerTests return (new WeatherController(getWeatherHttpClient.Object), getWeatherHttpClient); } - - - - - - - - - - - - - - - - - - - - - + [TestMethod] + public async Task WeatherController_GetCurrentTemp_NoZipCode_Returns400() + { + //Given an API call + //When asking for current temp and no zip code is given + //Then returns a 400 + } //[TestMethod] - //public async Task WeatherController_GetCurrentTemp_NoZipCode_Returns400() + //public async Task WeatherController_GetCurrentTemp_ZipCode_CallsWithZipCode() //{ - // // Arrange - // var (weatherController, getWeatherHttpClient) = Factory(); + //Assert.Inconclusive(); + //} - // // Act - // var result = await weatherController.CurrentTemp(0); + #region completed tests + /* - // // Assert - // Assert.AreEqual(400, (result.Result as BadRequestObjectResult).StatusCode); - //} + [TestMethod] + public async Task WeatherController_GetCurrentTemp_NoZipCode_Returns400() + { + // Arrange + var (weatherController, getWeatherHttpClient) = Factory(); - //[TestMethod] - //public async Task WeatherController_GetCurrentTemp_ZipCode_CallsWithZipCode() - //{ - // /** - // * Given an API call - // * When asking for current temp - // * Then it calls the weather Api with the correct zip code - // */ - // // Arrange - // var zipCode = 57105; - // var (weatherController, getWeatherHttpClient) = Factory(); - - // // fake the return from weather provider with MOQ - // var fakeTemp = 72.6; - // getWeatherHttpClient.Setup(wp => wp.GetCurrentTempAsync(zipCode)) - // .ReturnsAsync(fakeTemp); - - // // Act - // var response = await weatherController.CurrentTemp(zipCode); - - // // Assert - // getWeatherHttpClient.Verify(w => w.GetCurrentTempAsync(zipCode), Times.Once); - - // // Note this test may not be the most useful, but it helps move us forward with TDD - // // some may just delete this test or skip writting it - //} + // Act + var result = await weatherController.CurrentTemp(0); - //[TestMethod] - //public async Task WeatherController_GetTemp_NoZipCode_Returns400() - //{ - // // Arrange - // var (weatherController, getWeatherHttpClient) = Factory(); + // Assert + Assert.AreEqual(400, (result.Result as BadRequestObjectResult).StatusCode); + } - // // Act - // var result = await weatherController.CurrentTemp(0); + [TestMethod] + public async Task WeatherController_GetCurrentTemp_ZipCode_CallsWithZipCode() + { + /// + // Given an API call + // When asking for current temp + // Then it calls the weather Api with the correct zip code + /// + // Arrange + var zipCode = 57105; + var (weatherController, getWeatherHttpClient) = Factory(); + + // fake the return from weather provider with MOQ + var fakeTemp = 72.6; + getWeatherHttpClient.Setup(wp => wp.GetCurrentTempAsync(zipCode)) + .ReturnsAsync(fakeTemp); + + // Act + var response = await weatherController.CurrentTemp(zipCode); + + // Assert + getWeatherHttpClient.Verify(w => w.GetCurrentTempAsync(zipCode), Times.Once); + + // Note this test may not be the most useful, but it helps move us forward with TDD + // some may just delete this test or skip writting it + } - // // Assert - // Assert.AreEqual(400, (result.Result as BadRequestObjectResult).StatusCode); - //} + [TestMethod] + public async Task WeatherController_GetTemp_NoZipCode_Returns400() + { + // Arrange + var (weatherController, getWeatherHttpClient) = Factory(); - //[TestMethod] - //public async Task WeatherController_GetPastTemp_NoZipCode_Returns400() - //{ - // // Arrange - // var (weatherController, getWeatherHttpClient) = Factory(); + // Act + var result = await weatherController.CurrentTemp(0); - // // Act - // var result = await weatherController.PastWeather(0, string.Empty); + // Assert + Assert.AreEqual(400, (result.Result as BadRequestObjectResult).StatusCode); + } - // // Assert - // Assert.AreEqual(400, (result.Result as BadRequestObjectResult).StatusCode); - //} + [TestMethod] + public async Task WeatherController_GetPastTemp_NoZipCode_Returns400() + { + // Arrange + var (weatherController, getWeatherHttpClient) = Factory(); - //[TestMethod] - //[DataRow("")] - //[DataRow("00-1-2 10:00:00")] - //public async Task WeatherController_GetPastTemp_NoDateTimeOrInvalid_Returns400(string dateTime) - //{ - // // Arrange - // var (weatherController, getWeatherHttpClient) = Factory(); + // Act + var result = await weatherController.PastWeather(0, string.Empty); - // // Act - // var result = await weatherController.PastWeather(59785, string.Empty); + // Assert + Assert.AreEqual(400, (result.Result as BadRequestObjectResult).StatusCode); + } - // // Assert - // Assert.AreEqual(400, (result.Result as BadRequestObjectResult).StatusCode); - //} + [TestMethod] + [DataRow("")] + [DataRow("00-1-2 10:00:00")] + public async Task WeatherController_GetPastTemp_NoDateTimeOrInvalid_Returns400(string dateTime) + { + // Arrange + var (weatherController, getWeatherHttpClient) = Factory(); - //[TestMethod] - //public async Task WeatherController_GetPastTemp_ZipCodeAndDate_CallsProviderWithZipCodeAndDate() - //{ - // /** - // * Given an API call - // * When asking for past temp - // * Then it calls the weather Api with the correct zip code and date time - // */ - // // Arrange - // var zipCode = 57105; - // var date = DateTime.Now; - // var (weatherController, getWeatherHttpClient) = Factory(); - - // // fake the return from weather provider with MOQ - // var fakeTemp = 72.6; - // var fakeResponse = new ApiuxWeatherForecastResponse - // { - // Forecast = new ApiuxWeatherForecast - // { - // ForecastDay = new List { - // new ForecastDay - // { - // Hour = new List { - // new ForecastHour - // { - // FeelslikeF = fakeTemp - // } - // } - // } - // } - // } - // }; - // getWeatherHttpClient.Setup(wp => wp.GetPastWeatherAsync(It.IsAny(), It.IsAny())) - // //It.Is(d => d.ToString() == date.ToString()))) - // .ReturnsAsync(fakeResponse); - - // // Act - // var result = await weatherController.PastWeather(zipCode, date.ToString()); - - // // Assert - // getWeatherHttpClient.Verify(w => w.GetPastWeatherAsync(zipCode, - // // needs to use It.Is straight date doesn't match - // It.Is(d => d.ToString() == date.ToString())), - // Times.Once); - - // // this really only tests the MOQ - // //var weatherResult = ApiuxWeatherCurrentResponse.FromJson((result.Result as OkObjectResult).Value as string); - // //Assert.AreEqual(fakeResponse.Current.TempF, weatherResult.Current.TempF); - //} + // Act + var result = await weatherController.PastWeather(59785, string.Empty); + + // Assert + Assert.AreEqual(400, (result.Result as BadRequestObjectResult).StatusCode); + } + + [TestMethod] + public async Task WeatherController_GetPastTemp_ZipCodeAndDate_CallsProviderWithZipCodeAndDate() + { + // + // Given an API call + // When asking for past temp + // Then it calls the weather Api with the correct zip code and date time + // + // Arrange + var zipCode = 57105; + var date = DateTime.Now; + var (weatherController, getWeatherHttpClient) = Factory(); + + // fake the return from weather provider with MOQ + var fakeTemp = 72.6; + var fakeResponse = new ApiuxWeatherForecastResponse + { + Forecast = new ApiuxWeatherForecast + { + ForecastDay = new List { + new ForecastDay + { + Hour = new List { + new ForecastHour + { + FeelslikeF = fakeTemp + } + } + } + } + } + }; + getWeatherHttpClient.Setup(wp => wp.GetPastWeatherAsync(It.IsAny(), It.IsAny())) + //It.Is(d => d.ToString() == date.ToString()))) + .ReturnsAsync(fakeResponse); + + // Act + var result = await weatherController.PastWeather(zipCode, date.ToString()); + + // Assert + getWeatherHttpClient.Verify(w => w.GetPastWeatherAsync(zipCode, + // needs to use It.Is straight date doesn't match + It.Is(d => d.ToString() == date.ToString())), + Times.Once); + + // this really only tests the MOQ + //var weatherResult = ApiuxWeatherCurrentResponse.FromJson((result.Result as OkObjectResult).Value as string); + //Assert.AreEqual(fakeResponse.Current.TempF, weatherResult.Current.TempF); + } + */ + #endregion //// TODO add more tests and code if formatting the data response to abstract from Apiux is required } } diff --git a/source/api/Weather/WeatherController.cs b/source/api/Weather/WeatherController.cs index 3895991..3baf34f 100644 --- a/source/api/Weather/WeatherController.cs +++ b/source/api/Weather/WeatherController.cs @@ -17,11 +17,12 @@ public WeatherController(IGetWeatherHttpClient weatherHttpClient) [HttpGet("[action]")] public async Task> CurrentTemp([FromQuery(Name = "zipcode")] int zipCode) { + if (zipCode == 0) + { + return BadRequest($"{nameof(zipCode)} cannot be 0"); + } + return Ok(zipCode); - //if (zipCode == 0) - //{ - // return BadRequest($"{nameof(zipCode)} cannot be 0"); - //} //var result = await weatherHttpClient.GetCurrentTempAsync(zipCode); //return Ok(result); From 741e337f7e6574a928a127efb2560166acb8a6aa Mon Sep 17 00:00:00 2001 From: Kevin Logan Date: Tue, 29 Jan 2019 16:47:52 -0600 Subject: [PATCH 03/12] starting point for WeatherController tests --- .../Weather/WeatherControllerTests.cs | 52 +++++++++++++++++-- source/api/Weather/WeatherController.cs | 19 +++++-- 2 files changed, 62 insertions(+), 9 deletions(-) diff --git a/source/api.tests/Weather/WeatherControllerTests.cs b/source/api.tests/Weather/WeatherControllerTests.cs index eafdc80..3666ce4 100644 --- a/source/api.tests/Weather/WeatherControllerTests.cs +++ b/source/api.tests/Weather/WeatherControllerTests.cs @@ -22,16 +22,19 @@ public async Task WeatherController_GetCurrentTemp_NoZipCode_Returns400() //Given an API call //When asking for current temp and no zip code is given //Then returns a 400 + Assert.Inconclusive(); } - //[TestMethod] //public async Task WeatherController_GetCurrentTemp_ZipCode_CallsWithZipCode() //{ - //Assert.Inconclusive(); + // // Given an API call + // // When asking for current temp + // // Then it calls the weather Api with the correct zip code + // Assert.Inconclusive(); //} - #region completed tests + #region first tests /* [TestMethod] @@ -46,6 +49,44 @@ public async Task WeatherController_GetCurrentTemp_NoZipCode_Returns400() // Assert Assert.AreEqual(400, (result.Result as BadRequestObjectResult).StatusCode); } + + [TestMethod] + [DataRow(0)] + [DataRow(-57105)] + [DataRow(12)] + [DataRow(1921393)] + public async Task WeatherController_GetCurrentTemp_NoOrBadZipCode_Returns400(int zipCode) + { + //Given an API call + //When asking for current temp and no zip code is given + //Then returns a 400 + // Arrange + var (weatherController, getWeatherHttpClient) = Factory(); + + // Act + var result = await weatherController.CurrentTemp(zipCode); + + // Assert + Assert.AreEqual(400, (result.Result as BadRequestObjectResult).StatusCode); + } + + + + [TestMethod] + public async Task WeatherController_GetCurrentTemp_NoOrBadZipCode_Returns400() + { + //Given an API call + //When asking for current temp and no zip code is given + //Then returns a 400 + // Arrange + var (weatherController, getWeatherHttpClient) = Factory(); + + // Act + var result = await weatherController.CurrentTemp(0); + + // Assert + Assert.AreEqual(400, (result.Result as BadRequestObjectResult).StatusCode); + } [TestMethod] public async Task WeatherController_GetCurrentTemp_ZipCode_CallsWithZipCode() @@ -73,7 +114,10 @@ public async Task WeatherController_GetCurrentTemp_ZipCode_CallsWithZipCode() // Note this test may not be the most useful, but it helps move us forward with TDD // some may just delete this test or skip writting it } - + */ + #endregion + #region completed tests + /* [TestMethod] public async Task WeatherController_GetTemp_NoZipCode_Returns400() { diff --git a/source/api/Weather/WeatherController.cs b/source/api/Weather/WeatherController.cs index 3baf34f..72e5801 100644 --- a/source/api/Weather/WeatherController.cs +++ b/source/api/Weather/WeatherController.cs @@ -15,15 +15,24 @@ public WeatherController(IGetWeatherHttpClient weatherHttpClient) } [HttpGet("[action]")] - public async Task> CurrentTemp([FromQuery(Name = "zipcode")] int zipCode) + public async Task> CurrentTemp([FromQuery] int zipCode) { - if (zipCode == 0) - { - return BadRequest($"{nameof(zipCode)} cannot be 0"); - } + // starting code + //if (zipCode == 0) + //{ + // return BadRequest($"{nameof(zipCode)} is not valid"); + //} return Ok(zipCode); + + // final product + // http://structnet.com/instructions/zip_min_max_by_state.html + //if (zipCode <= 1000 || zipCode > 99999) + //{ + // return BadRequest($"{nameof(zipCode)} cannot be 0"); + //} + //var result = await weatherHttpClient.GetCurrentTempAsync(zipCode); //return Ok(result); } From 9905710ddb5c4f6f19b93a7f784a6d08da399700 Mon Sep 17 00:00:00 2001 From: Kevin Logan Date: Tue, 5 Feb 2019 13:21:54 -0600 Subject: [PATCH 04/12] improvments after 2/5 run through --- source/api.tests/Weather/ApixuClientTests.cs | 30 +++++++++-- .../Weather/WeatherControllerTests.cs | 51 +++++++++++++------ source/api/Exceptions/NotFoundException.cs | 15 ++++++ source/api/Weather/ApixuClient.cs | 16 +++++- 4 files changed, 91 insertions(+), 21 deletions(-) create mode 100644 source/api/Exceptions/NotFoundException.cs diff --git a/source/api.tests/Weather/ApixuClientTests.cs b/source/api.tests/Weather/ApixuClientTests.cs index 6a8f0ab..2716fd0 100644 --- a/source/api.tests/Weather/ApixuClientTests.cs +++ b/source/api.tests/Weather/ApixuClientTests.cs @@ -21,8 +21,6 @@ public class ApixuClientTests } - - //[TestMethod] //public async Task ApixuClientTests_GetTemp_GivenAZipCode_ReturnsTemp() //{ @@ -48,6 +46,32 @@ public class ApixuClientTests // Assert.AreEqual(fakeTemp, result); //} + //[TestMethod] + //public async Task ApixuClientTests_GetTemp_GivenAZipCode_NotFound_ThrowsException() + //{ + // // how do we handle error states outside our control? + // // Arrange + // var (apiuxClient, mockHttp) = Factory(); + // const int zipCode = 57105; + // const double fakeTemp = 70.5; + // var response = new ApiuxWeatherCurrentResponse + // { + // Current = new ApiuxWeatherCurrent + // { + // TempF = fakeTemp + // } + // }; + // var requestUri = $"https://api.apixu.com/v1/current.json?key={ApixuClient.apiKey}&q={zipCode}"; + // var request = mockHttp.When(requestUri) + // .Respond(HttpStatusCode.NotFound); + + // // Act and Assert + // await Assert.ThrowsExceptionAsync(async () => + // { + // await apiuxClient.GetCurrentTempAsync(zipCode); + // }); + //} + //[TestMethod] //public async Task ApixuClientTests_GetTemp_GivenAZipCode_UsesThatZipCode() //{ @@ -66,7 +90,7 @@ public class ApixuClientTests // var request = mockHttp.When(requestUri) // .Respond("application/json", Serialize.ToJson(response)); - + // // Act // var result = await apiuxClient.GetCurrentTempAsync(zipCode); diff --git a/source/api.tests/Weather/WeatherControllerTests.cs b/source/api.tests/Weather/WeatherControllerTests.cs index 3666ce4..82038d1 100644 --- a/source/api.tests/Weather/WeatherControllerTests.cs +++ b/source/api.tests/Weather/WeatherControllerTests.cs @@ -48,14 +48,10 @@ public async Task WeatherController_GetCurrentTemp_NoZipCode_Returns400() // Assert Assert.AreEqual(400, (result.Result as BadRequestObjectResult).StatusCode); - } - + } + [TestMethod] - [DataRow(0)] - [DataRow(-57105)] - [DataRow(12)] - [DataRow(1921393)] - public async Task WeatherController_GetCurrentTemp_NoOrBadZipCode_Returns400(int zipCode) + public async Task WeatherController_GetCurrentTemp_NoOrBadZipCode_Returns400() { //Given an API call //When asking for current temp and no zip code is given @@ -64,16 +60,19 @@ public async Task WeatherController_GetCurrentTemp_NoOrBadZipCode_Returns400(int var (weatherController, getWeatherHttpClient) = Factory(); // Act - var result = await weatherController.CurrentTemp(zipCode); + var result = await weatherController.CurrentTemp(0); // Assert Assert.AreEqual(400, (result.Result as BadRequestObjectResult).StatusCode); } - - - + + // replaces WeatherController_GetCurrentTemp_NoOrBadZipCode_Returns400 [TestMethod] - public async Task WeatherController_GetCurrentTemp_NoOrBadZipCode_Returns400() + [DataRow(0)] + [DataRow(-57105)] + [DataRow(12)] + [DataRow(1921393)] + public async Task WeatherController_GetCurrentTemp_NoOrBadZipCode_Returns400(int zipCode) { //Given an API call //When asking for current temp and no zip code is given @@ -82,22 +81,22 @@ public async Task WeatherController_GetCurrentTemp_NoOrBadZipCode_Returns400() var (weatherController, getWeatherHttpClient) = Factory(); // Act - var result = await weatherController.CurrentTemp(0); + var result = await weatherController.CurrentTemp(zipCode); // Assert Assert.AreEqual(400, (result.Result as BadRequestObjectResult).StatusCode); } [TestMethod] - public async Task WeatherController_GetCurrentTemp_ZipCode_CallsWithZipCode() + public async Task WeatherController_GetCurrentTemp_ValidZipCode_CallsWithZipCode() { /// // Given an API call // When asking for current temp // Then it calls the weather Api with the correct zip code /// - // Arrange - var zipCode = 57105; + // Arrange + var zipCode = 57105; var (weatherController, getWeatherHttpClient) = Factory(); // fake the return from weather provider with MOQ @@ -113,6 +112,26 @@ public async Task WeatherController_GetCurrentTemp_ZipCode_CallsWithZipCode() // Note this test may not be the most useful, but it helps move us forward with TDD // some may just delete this test or skip writting it + } + + + [TestMethod] + public async Task WeatherController_GetCurrentTemp_ValidZipCode_ReturnsTheTemp() + { + // first make/leave WeatherController return zipcode in result + // Arrange + var zipCode = 57105; + var (weatherController, getWeatherHttpClient) = Factory(); + var fakeTemp = 72.6; + getWeatherHttpClient.Setup(wp => wp.GetCurrentTempAsync(zipCode)) + .ReturnsAsync(fakeTemp); + + // Act + var result = await weatherController.CurrentTemp(zipCode); + + // Assert + Assert.AreEqual(200, (result.Result as OkObjectResult).StatusCode); + Assert.AreEqual(fakeTemp, (result.Result as OkObjectResult).Value); } */ #endregion diff --git a/source/api/Exceptions/NotFoundException.cs b/source/api/Exceptions/NotFoundException.cs new file mode 100644 index 0000000..d126581 --- /dev/null +++ b/source/api/Exceptions/NotFoundException.cs @@ -0,0 +1,15 @@ +using System; + +namespace Api.Weather.Exceptions +{ + /// + /// Custom exception for something that wasn't found + /// + [Serializable] + public class NotFoundException : Exception + { + public NotFoundException(string message) : base(message) + { + } + } +} \ No newline at end of file diff --git a/source/api/Weather/ApixuClient.cs b/source/api/Weather/ApixuClient.cs index 6cd1ed6..33777a1 100644 --- a/source/api/Weather/ApixuClient.cs +++ b/source/api/Weather/ApixuClient.cs @@ -1,4 +1,5 @@ -using System; +using Api.Weather.Exceptions; +using System; using System.Net.Http; using System.Net.Http.Headers; using System.Threading.Tasks; @@ -27,7 +28,18 @@ public ApixuClient(HttpClient httpClient) public async Task GetCurrentTempAsync(int zipCode) { - var response = await httpClient.GetStringAsync($"current.json?key={apiKey}&q={zipCode}"); + string response = string.Empty; + //try + //{ + response = await httpClient.GetStringAsync($"current.json?key={apiKey}&q={zipCode}"); + //} + //catch(HttpRequestException) + //{ + // for ApixuClientTests_GetTemp_GivenAZipCode_NotFound_ThrowsException discussion + // // TODO check the body of the exception + // throw new NotFoundException($"{zipCode} didn't work"); + //} + var weather = ApiuxWeatherCurrentResponse.FromJson(response); return weather.Current.TempF; } From 0aca41bd44130b0a50b9704202301f103a90a6f3 Mon Sep 17 00:00:00 2001 From: Kevin Logan Date: Wed, 15 May 2019 13:45:34 -0500 Subject: [PATCH 05/12] reminder comment to make zip into string --- source/api/Weather/ApixuClient.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/source/api/Weather/ApixuClient.cs b/source/api/Weather/ApixuClient.cs index 33777a1..2614967 100644 --- a/source/api/Weather/ApixuClient.cs +++ b/source/api/Weather/ApixuClient.cs @@ -28,6 +28,7 @@ public ApixuClient(HttpClient httpClient) public async Task GetCurrentTempAsync(int zipCode) { + // suggestion is to make zipCode into a string and make it postal code instead string response = string.Empty; //try //{ From e3a05d3643582c1acea29ff122c0b119694d706c Mon Sep 17 00:00:00 2001 From: Kevin Logan Date: Mon, 29 Jul 2019 14:06:03 -0500 Subject: [PATCH 06/12] comment about using props instead of tuple --- source/api.tests/Weather/WeatherControllerTests.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/source/api.tests/Weather/WeatherControllerTests.cs b/source/api.tests/Weather/WeatherControllerTests.cs index 82038d1..829b2ff 100644 --- a/source/api.tests/Weather/WeatherControllerTests.cs +++ b/source/api.tests/Weather/WeatherControllerTests.cs @@ -12,6 +12,9 @@ public class WeatherControllerTests { private (WeatherController weatherController, Mock getWeatherHttpClient) Factory() { + // an alternative to returning the tuple, is to have private properties on this class for the injected classes + // this is useful if you're class/unit under test has too many dependencies + // which means you should probably refactor that class to separate out concerns var getWeatherHttpClient = new Mock(); return (new WeatherController(getWeatherHttpClient.Object), getWeatherHttpClient); } From 5796f2eea4977d43d69859ce8bfb3a5f14f4e0b6 Mon Sep 17 00:00:00 2001 From: Kevin Logan Date: Mon, 12 Aug 2019 21:57:20 -0500 Subject: [PATCH 07/12] more comments and prep for NE Code --- .../Weather/WeatherControllerTests.cs | 31 +++++++------------ 1 file changed, 12 insertions(+), 19 deletions(-) diff --git a/source/api.tests/Weather/WeatherControllerTests.cs b/source/api.tests/Weather/WeatherControllerTests.cs index 829b2ff..da5bab9 100644 --- a/source/api.tests/Weather/WeatherControllerTests.cs +++ b/source/api.tests/Weather/WeatherControllerTests.cs @@ -25,18 +25,9 @@ public async Task WeatherController_GetCurrentTemp_NoZipCode_Returns400() //Given an API call //When asking for current temp and no zip code is given //Then returns a 400 - Assert.Inconclusive(); + Assert.Fail(); } - //[TestMethod] - //public async Task WeatherController_GetCurrentTemp_ZipCode_CallsWithZipCode() - //{ - // // Given an API call - // // When asking for current temp - // // Then it calls the weather Api with the correct zip code - // Assert.Inconclusive(); - //} - #region first tests /* @@ -46,6 +37,8 @@ public async Task WeatherController_GetCurrentTemp_NoZipCode_Returns400() // Arrange var (weatherController, getWeatherHttpClient) = Factory(); + // we won't get to the client call, so no stubs setup needed + // Act var result = await weatherController.CurrentTemp(0); @@ -69,7 +62,8 @@ public async Task WeatherController_GetCurrentTemp_NoOrBadZipCode_Returns400() Assert.AreEqual(400, (result.Result as BadRequestObjectResult).StatusCode); } - // replaces WeatherController_GetCurrentTemp_NoOrBadZipCode_Returns400 + // now, let's learn how to use DataRow + // and replace WeatherController_GetCurrentTemp_NoOrBadZipCode_Returns400 [TestMethod] [DataRow(0)] [DataRow(-57105)] @@ -93,16 +87,15 @@ public async Task WeatherController_GetCurrentTemp_NoOrBadZipCode_Returns400(int [TestMethod] public async Task WeatherController_GetCurrentTemp_ValidZipCode_CallsWithZipCode() { - /// - // Given an API call - // When asking for current temp - // Then it calls the weather Api with the correct zip code - /// + // Given an API call + // When asking for current temp + // Then it calls the weather Api with the correct zip code + // Arrange var zipCode = 57105; var (weatherController, getWeatherHttpClient) = Factory(); - // fake the return from weather provider with MOQ + // setup the stub and fake the return from weather provider with MOQ var fakeTemp = 72.6; getWeatherHttpClient.Setup(wp => wp.GetCurrentTempAsync(zipCode)) .ReturnsAsync(fakeTemp); @@ -113,7 +106,7 @@ public async Task WeatherController_GetCurrentTemp_ValidZipCode_CallsWithZipCode // Assert getWeatherHttpClient.Verify(w => w.GetCurrentTempAsync(zipCode), Times.Once); - // Note this test may not be the most useful, but it helps move us forward with TDD + // Note: this test may not be the most useful, but it helps move us forward with TDD // some may just delete this test or skip writting it } @@ -121,7 +114,7 @@ public async Task WeatherController_GetCurrentTemp_ValidZipCode_CallsWithZipCode [TestMethod] public async Task WeatherController_GetCurrentTemp_ValidZipCode_ReturnsTheTemp() { - // first make/leave WeatherController return zipcode in result + // first make/leave WeatherController return zipcode in result // Arrange var zipCode = 57105; var (weatherController, getWeatherHttpClient) = Factory(); From 89443552c34a319ad72d0a6adcc0790debc772ea Mon Sep 17 00:00:00 2001 From: Kevin Logan Date: Mon, 12 Aug 2019 21:59:29 -0500 Subject: [PATCH 08/12] more comments --- source/api.tests/Weather/WeatherControllerTests.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/source/api.tests/Weather/WeatherControllerTests.cs b/source/api.tests/Weather/WeatherControllerTests.cs index da5bab9..45dea22 100644 --- a/source/api.tests/Weather/WeatherControllerTests.cs +++ b/source/api.tests/Weather/WeatherControllerTests.cs @@ -30,7 +30,9 @@ public async Task WeatherController_GetCurrentTemp_NoZipCode_Returns400() #region first tests /* - + // let's start with the not allowed, those are easy to think of + // (work as a team with QA to come up with these before you start coding + // at least before you consider your work as done) [TestMethod] public async Task WeatherController_GetCurrentTemp_NoZipCode_Returns400() { From a6e5769d353b3588b76c3c1e98c48b4b18e7f916 Mon Sep 17 00:00:00 2001 From: Kevin Logan Date: Mon, 12 Aug 2019 22:01:13 -0500 Subject: [PATCH 09/12] more comments --- source/api.tests/Weather/WeatherControllerTests.cs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/source/api.tests/Weather/WeatherControllerTests.cs b/source/api.tests/Weather/WeatherControllerTests.cs index 45dea22..a9e6189 100644 --- a/source/api.tests/Weather/WeatherControllerTests.cs +++ b/source/api.tests/Weather/WeatherControllerTests.cs @@ -64,7 +64,7 @@ public async Task WeatherController_GetCurrentTemp_NoOrBadZipCode_Returns400() Assert.AreEqual(400, (result.Result as BadRequestObjectResult).StatusCode); } - // now, let's learn how to use DataRow + // now, let's learn how to use DataRow to get more coverage // and replace WeatherController_GetCurrentTemp_NoOrBadZipCode_Returns400 [TestMethod] [DataRow(0)] @@ -86,6 +86,7 @@ public async Task WeatherController_GetCurrentTemp_NoOrBadZipCode_Returns400(int Assert.AreEqual(400, (result.Result as BadRequestObjectResult).StatusCode); } + // this shows how to setup the stub and fake the return from weather provider [TestMethod] public async Task WeatherController_GetCurrentTemp_ValidZipCode_CallsWithZipCode() { @@ -112,7 +113,7 @@ public async Task WeatherController_GetCurrentTemp_ValidZipCode_CallsWithZipCode // some may just delete this test or skip writting it } - + // test the happy path [TestMethod] public async Task WeatherController_GetCurrentTemp_ValidZipCode_ReturnsTheTemp() { @@ -131,6 +132,8 @@ public async Task WeatherController_GetCurrentTemp_ValidZipCode_ReturnsTheTemp() Assert.AreEqual(200, (result.Result as OkObjectResult).StatusCode); Assert.AreEqual(fakeTemp, (result.Result as OkObjectResult).Value); } + + // more tests could explore exception handling */ #endregion #region completed tests From cb074aaecfa08b22a7a13d4626f99d348b2e6355 Mon Sep 17 00:00:00 2001 From: Kevin Logan Date: Thu, 15 Aug 2019 16:41:30 -0500 Subject: [PATCH 10/12] comment fix --- source/api.tests/Weather/WeatherControllerTests.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/source/api.tests/Weather/WeatherControllerTests.cs b/source/api.tests/Weather/WeatherControllerTests.cs index a9e6189..854ed58 100644 --- a/source/api.tests/Weather/WeatherControllerTests.cs +++ b/source/api.tests/Weather/WeatherControllerTests.cs @@ -25,12 +25,14 @@ public async Task WeatherController_GetCurrentTemp_NoZipCode_Returns400() //Given an API call //When asking for current temp and no zip code is given //Then returns a 400 + + Assert.Fail(); } #region first tests /* - // let's start with the not allowed, those are easy to think of + // let's start with the not allowed use case, those are easy to think of // (work as a team with QA to come up with these before you start coding // at least before you consider your work as done) [TestMethod] From de9756be624ba7f9d7bb91f061a4d3600b70a978 Mon Sep 17 00:00:00 2001 From: Kevin Logan Date: Thu, 15 Aug 2019 20:37:10 -0500 Subject: [PATCH 11/12] /* comments for collapsing --- .../Weather/WeatherControllerTests.cs | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/source/api.tests/Weather/WeatherControllerTests.cs b/source/api.tests/Weather/WeatherControllerTests.cs index 854ed58..58e5ca0 100644 --- a/source/api.tests/Weather/WeatherControllerTests.cs +++ b/source/api.tests/Weather/WeatherControllerTests.cs @@ -22,11 +22,9 @@ public class WeatherControllerTests [TestMethod] public async Task WeatherController_GetCurrentTemp_NoZipCode_Returns400() { - //Given an API call - //When asking for current temp and no zip code is given - //Then returns a 400 - - + // Given an API call + // When asking for current temp and no zip code is given + // Then returns a 400 Assert.Fail(); } @@ -49,7 +47,8 @@ public async Task WeatherController_GetCurrentTemp_NoZipCode_Returns400() // Assert Assert.AreEqual(400, (result.Result as BadRequestObjectResult).StatusCode); } - + */ + /* [TestMethod] public async Task WeatherController_GetCurrentTemp_NoOrBadZipCode_Returns400() { @@ -65,7 +64,8 @@ public async Task WeatherController_GetCurrentTemp_NoOrBadZipCode_Returns400() // Assert Assert.AreEqual(400, (result.Result as BadRequestObjectResult).StatusCode); } - + */ + /* // now, let's learn how to use DataRow to get more coverage // and replace WeatherController_GetCurrentTemp_NoOrBadZipCode_Returns400 [TestMethod] @@ -87,7 +87,8 @@ public async Task WeatherController_GetCurrentTemp_NoOrBadZipCode_Returns400(int // Assert Assert.AreEqual(400, (result.Result as BadRequestObjectResult).StatusCode); } - + */ + /* // this shows how to setup the stub and fake the return from weather provider [TestMethod] public async Task WeatherController_GetCurrentTemp_ValidZipCode_CallsWithZipCode() @@ -114,7 +115,8 @@ public async Task WeatherController_GetCurrentTemp_ValidZipCode_CallsWithZipCode // Note: this test may not be the most useful, but it helps move us forward with TDD // some may just delete this test or skip writting it } - + */ + /* // test the happy path [TestMethod] public async Task WeatherController_GetCurrentTemp_ValidZipCode_ReturnsTheTemp() @@ -134,9 +136,9 @@ public async Task WeatherController_GetCurrentTemp_ValidZipCode_ReturnsTheTemp() Assert.AreEqual(200, (result.Result as OkObjectResult).StatusCode); Assert.AreEqual(fakeTemp, (result.Result as OkObjectResult).Value); } - - // more tests could explore exception handling */ + // more tests could explore exception handling + #endregion #region completed tests /* From 2b72fa3d400880a5447e93d75c36cee328de5577 Mon Sep 17 00:00:00 2001 From: Kevin Logan Date: Fri, 16 Aug 2019 11:00:38 -0500 Subject: [PATCH 12/12] GetCurrentTemp should be "CurrentTemp" --- source/api.tests/Weather/WeatherControllerTests.cs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/source/api.tests/Weather/WeatherControllerTests.cs b/source/api.tests/Weather/WeatherControllerTests.cs index 58e5ca0..7bb7dd9 100644 --- a/source/api.tests/Weather/WeatherControllerTests.cs +++ b/source/api.tests/Weather/WeatherControllerTests.cs @@ -20,7 +20,7 @@ public class WeatherControllerTests } [TestMethod] - public async Task WeatherController_GetCurrentTemp_NoZipCode_Returns400() + public async Task WeatherController_CurrentTemp_NoZipCode_Returns400() { // Given an API call // When asking for current temp and no zip code is given @@ -34,7 +34,7 @@ public async Task WeatherController_GetCurrentTemp_NoZipCode_Returns400() // (work as a team with QA to come up with these before you start coding // at least before you consider your work as done) [TestMethod] - public async Task WeatherController_GetCurrentTemp_NoZipCode_Returns400() + public async Task WeatherController_CurrentTemp_NoZipCode_Returns400() { // Arrange var (weatherController, getWeatherHttpClient) = Factory(); @@ -50,7 +50,7 @@ public async Task WeatherController_GetCurrentTemp_NoZipCode_Returns400() */ /* [TestMethod] - public async Task WeatherController_GetCurrentTemp_NoOrBadZipCode_Returns400() + public async Task WeatherController_CurrentTemp_NoOrBadZipCode_Returns400() { //Given an API call //When asking for current temp and no zip code is given @@ -67,13 +67,13 @@ public async Task WeatherController_GetCurrentTemp_NoOrBadZipCode_Returns400() */ /* // now, let's learn how to use DataRow to get more coverage - // and replace WeatherController_GetCurrentTemp_NoOrBadZipCode_Returns400 + // and replace WeatherController_CurrentTemp_NoOrBadZipCode_Returns400 [TestMethod] [DataRow(0)] [DataRow(-57105)] [DataRow(12)] [DataRow(1921393)] - public async Task WeatherController_GetCurrentTemp_NoOrBadZipCode_Returns400(int zipCode) + public async Task WeatherController_CurrentTemp_NoOrBadZipCode_Returns400(int zipCode) { //Given an API call //When asking for current temp and no zip code is given @@ -91,7 +91,7 @@ public async Task WeatherController_GetCurrentTemp_NoOrBadZipCode_Returns400(int /* // this shows how to setup the stub and fake the return from weather provider [TestMethod] - public async Task WeatherController_GetCurrentTemp_ValidZipCode_CallsWithZipCode() + public async Task WeatherController_CurrentTemp_ValidZipCode_CallsWithZipCode() { // Given an API call // When asking for current temp @@ -119,7 +119,7 @@ public async Task WeatherController_GetCurrentTemp_ValidZipCode_CallsWithZipCode /* // test the happy path [TestMethod] - public async Task WeatherController_GetCurrentTemp_ValidZipCode_ReturnsTheTemp() + public async Task WeatherController_CurrentTemp_ValidZipCode_ReturnsTheTemp() { // first make/leave WeatherController return zipcode in result // Arrange