From 92d18aa4848fb9150d3a35434414b375f21f0b88 Mon Sep 17 00:00:00 2001 From: StevanFreeborn Date: Mon, 11 Jul 2022 09:36:39 -0500 Subject: [PATCH] continuing to write tests --- server.tests/Http/HttpClientFactory.cs | 2 +- .../CharactersControllerIntegrationTests.cs | 2 +- .../EpisodesControllerIntegrationTests.cs | 2 +- .../QuotesControllerIntegrationTests.cs | 2 +- .../SeasonsControllerIntegrationTests.cs | 2 +- .../unitTests/SeasonsControllerUnitTests.cs | 62 +++++++++++++++++-- 6 files changed, 63 insertions(+), 9 deletions(-) diff --git a/server.tests/Http/HttpClientFactory.cs b/server.tests/Http/HttpClientFactory.cs index 0b6f658..fbfb464 100644 --- a/server.tests/Http/HttpClientFactory.cs +++ b/server.tests/Http/HttpClientFactory.cs @@ -4,7 +4,7 @@ namespace server.tests.Http { internal static class HttpClientFactory { - public static HttpClient GetHttpClient(int version) + public static HttpClient GetClient(int version) { var webAppFactory = new WebApplicationFactory(); diff --git a/server.tests/v1/integrationTests/CharactersControllerIntegrationTests.cs b/server.tests/v1/integrationTests/CharactersControllerIntegrationTests.cs index eed7250..708b2b0 100644 --- a/server.tests/v1/integrationTests/CharactersControllerIntegrationTests.cs +++ b/server.tests/v1/integrationTests/CharactersControllerIntegrationTests.cs @@ -19,7 +19,7 @@ namespace server.tests.v1.IntegrationTests public CharactersControllerIntegrationTests() { - _client = HttpClientFactory.GetHttpClient(1); + _client = HttpClientFactory.GetClient(1); _serializerOptions = OptionsFactory.GetJsonSerializerOptions(); diff --git a/server.tests/v1/integrationTests/EpisodesControllerIntegrationTests.cs b/server.tests/v1/integrationTests/EpisodesControllerIntegrationTests.cs index c9c8c26..0abd42b 100644 --- a/server.tests/v1/integrationTests/EpisodesControllerIntegrationTests.cs +++ b/server.tests/v1/integrationTests/EpisodesControllerIntegrationTests.cs @@ -17,7 +17,7 @@ namespace server.tests.v1.IntegrationTests public EpisodesControllerIntegrationTests() { - _client = HttpClientFactory.GetHttpClient(1); + _client = HttpClientFactory.GetClient(1); _serializerOptions = OptionsFactory.GetJsonSerializerOptions(); diff --git a/server.tests/v1/integrationTests/QuotesControllerIntegrationTests.cs b/server.tests/v1/integrationTests/QuotesControllerIntegrationTests.cs index bdaa319..d078d07 100644 --- a/server.tests/v1/integrationTests/QuotesControllerIntegrationTests.cs +++ b/server.tests/v1/integrationTests/QuotesControllerIntegrationTests.cs @@ -12,7 +12,7 @@ namespace server.tests.v1.integrationTests public QuotesControllerIntegrationTests() { - _client = HttpClientFactory.GetHttpClient(1); + _client = HttpClientFactory.GetClient(1); _serializerOptions = OptionsFactory.GetJsonSerializerOptions(); diff --git a/server.tests/v1/integrationTests/SeasonsControllerIntegrationTests.cs b/server.tests/v1/integrationTests/SeasonsControllerIntegrationTests.cs index eae7ed7..39b7194 100644 --- a/server.tests/v1/integrationTests/SeasonsControllerIntegrationTests.cs +++ b/server.tests/v1/integrationTests/SeasonsControllerIntegrationTests.cs @@ -12,7 +12,7 @@ namespace server.tests.v1.integrationTests public SeasonsControllerIntegrationTests() { - _client = HttpClientFactory.GetHttpClient(1); + _client = HttpClientFactory.GetClient(1); _serializerOptions = OptionsFactory.GetJsonSerializerOptions(); diff --git a/server.tests/v1/unitTests/SeasonsControllerUnitTests.cs b/server.tests/v1/unitTests/SeasonsControllerUnitTests.cs index b8ec7bb..70c6d74 100644 --- a/server.tests/v1/unitTests/SeasonsControllerUnitTests.cs +++ b/server.tests/v1/unitTests/SeasonsControllerUnitTests.cs @@ -1,6 +1,10 @@ -using Moq; +using FluentAssertions; +using Microsoft.AspNetCore.Mvc; +using Moq; using server.Controllers.v1; +using server.Models; using server.Persistence.Repositories; +using System.Net; namespace server.tests.v1.unitTests { @@ -18,13 +22,46 @@ namespace server.tests.v1.unitTests [Fact] public async Task GetSeasonsAsync_AllSeasons_Returns200StatusCodeWithSeasonsCollection() { - throw new NotImplementedException(); + var seasons = new List { new Season(), new Season() }; + + _mockRepo + .Setup(repo => repo.GetSeasonsAsync()) + .ReturnsAsync(seasons); + + var response = await _controller.GetSeasonsAsync() as ObjectResult; + + var data = response.Value as List; + + _mockRepo.Verify(repo => repo.GetSeasonsAsync(), Times.Once()); + + response.Should().NotBeNull(); + response.Should().BeOfType(); + response.StatusCode.Should().Be((int)HttpStatusCode.OK); + + data.Should().NotBeNull(); + data.Should().BeOfType>(); + data.Should().HaveCount(2); } [Fact] public async Task GetSeasonsAsync_RepoThrowsException_Returns500StatusCodeWithProblemDetails() { - throw new NotImplementedException(); + _mockRepo + .Setup(repo => repo.GetSeasonsAsync()) + .Throws(new Exception()); + + var response = await _controller.GetSeasonsAsync() as ObjectResult; + + var details = response.Value; + + _mockRepo.Verify(repo => repo.GetSeasonsAsync(), Times.Once()); + + response.Should().NotBeNull(); + response.Should().BeOfType(); + response.StatusCode.Should().Be((int)HttpStatusCode.InternalServerError); + + details.Should().NotBeNull(); + details.Should().BeOfType(); } [Fact] @@ -42,7 +79,24 @@ namespace server.tests.v1.unitTests [Fact] public async Task GetSeasonByNumberAsync_RepoThrowsException_Returns500StatusCodeWithProblemDetails() { - throw new NotImplementedException(); + var seasonNumber = 1; + + _mockRepo + .Setup(repo => repo.GetSeasonByNumberAsync(seasonNumber)) + .Throws(new Exception()); + + var response = await _controller.GetSeasonByNumberAsync(seasonNumber) as ObjectResult; + + var details = response.Value; + + _mockRepo.Verify(repo => repo.GetSeasonByNumberAsync(It.IsAny()), Times.Once()); + + response.Should().NotBeNull(); + response.Should().BeOfType(); + response.StatusCode.Should().Be((int)HttpStatusCode.InternalServerError); + + details.Should().NotBeNull(); + details.Should().BeOfType(); } } }