continuing to write tests
This commit is contained in:
@@ -4,7 +4,7 @@ namespace server.tests.Http
|
|||||||
{
|
{
|
||||||
internal static class HttpClientFactory
|
internal static class HttpClientFactory
|
||||||
{
|
{
|
||||||
public static HttpClient GetHttpClient(int version)
|
public static HttpClient GetClient(int version)
|
||||||
{
|
{
|
||||||
var webAppFactory = new WebApplicationFactory<Program>();
|
var webAppFactory = new WebApplicationFactory<Program>();
|
||||||
|
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ namespace server.tests.v1.IntegrationTests
|
|||||||
|
|
||||||
public CharactersControllerIntegrationTests()
|
public CharactersControllerIntegrationTests()
|
||||||
{
|
{
|
||||||
_client = HttpClientFactory.GetHttpClient(1);
|
_client = HttpClientFactory.GetClient(1);
|
||||||
|
|
||||||
_serializerOptions = OptionsFactory.GetJsonSerializerOptions();
|
_serializerOptions = OptionsFactory.GetJsonSerializerOptions();
|
||||||
|
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ namespace server.tests.v1.IntegrationTests
|
|||||||
|
|
||||||
public EpisodesControllerIntegrationTests()
|
public EpisodesControllerIntegrationTests()
|
||||||
{
|
{
|
||||||
_client = HttpClientFactory.GetHttpClient(1);
|
_client = HttpClientFactory.GetClient(1);
|
||||||
|
|
||||||
_serializerOptions = OptionsFactory.GetJsonSerializerOptions();
|
_serializerOptions = OptionsFactory.GetJsonSerializerOptions();
|
||||||
|
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ namespace server.tests.v1.integrationTests
|
|||||||
|
|
||||||
public QuotesControllerIntegrationTests()
|
public QuotesControllerIntegrationTests()
|
||||||
{
|
{
|
||||||
_client = HttpClientFactory.GetHttpClient(1);
|
_client = HttpClientFactory.GetClient(1);
|
||||||
|
|
||||||
_serializerOptions = OptionsFactory.GetJsonSerializerOptions();
|
_serializerOptions = OptionsFactory.GetJsonSerializerOptions();
|
||||||
|
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ namespace server.tests.v1.integrationTests
|
|||||||
|
|
||||||
public SeasonsControllerIntegrationTests()
|
public SeasonsControllerIntegrationTests()
|
||||||
{
|
{
|
||||||
_client = HttpClientFactory.GetHttpClient(1);
|
_client = HttpClientFactory.GetClient(1);
|
||||||
|
|
||||||
_serializerOptions = OptionsFactory.GetJsonSerializerOptions();
|
_serializerOptions = OptionsFactory.GetJsonSerializerOptions();
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,10 @@
|
|||||||
using Moq;
|
using FluentAssertions;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Moq;
|
||||||
using server.Controllers.v1;
|
using server.Controllers.v1;
|
||||||
|
using server.Models;
|
||||||
using server.Persistence.Repositories;
|
using server.Persistence.Repositories;
|
||||||
|
using System.Net;
|
||||||
|
|
||||||
namespace server.tests.v1.unitTests
|
namespace server.tests.v1.unitTests
|
||||||
{
|
{
|
||||||
@@ -18,13 +22,46 @@ namespace server.tests.v1.unitTests
|
|||||||
[Fact]
|
[Fact]
|
||||||
public async Task GetSeasonsAsync_AllSeasons_Returns200StatusCodeWithSeasonsCollection()
|
public async Task GetSeasonsAsync_AllSeasons_Returns200StatusCodeWithSeasonsCollection()
|
||||||
{
|
{
|
||||||
throw new NotImplementedException();
|
var seasons = new List<Season> { new Season(), new Season() };
|
||||||
|
|
||||||
|
_mockRepo
|
||||||
|
.Setup(repo => repo.GetSeasonsAsync())
|
||||||
|
.ReturnsAsync(seasons);
|
||||||
|
|
||||||
|
var response = await _controller.GetSeasonsAsync() as ObjectResult;
|
||||||
|
|
||||||
|
var data = response.Value as List<Season>;
|
||||||
|
|
||||||
|
_mockRepo.Verify(repo => repo.GetSeasonsAsync(), Times.Once());
|
||||||
|
|
||||||
|
response.Should().NotBeNull();
|
||||||
|
response.Should().BeOfType<OkObjectResult>();
|
||||||
|
response.StatusCode.Should().Be((int)HttpStatusCode.OK);
|
||||||
|
|
||||||
|
data.Should().NotBeNull();
|
||||||
|
data.Should().BeOfType<List<Season>>();
|
||||||
|
data.Should().HaveCount(2);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task GetSeasonsAsync_RepoThrowsException_Returns500StatusCodeWithProblemDetails()
|
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<ObjectResult>();
|
||||||
|
response.StatusCode.Should().Be((int)HttpStatusCode.InternalServerError);
|
||||||
|
|
||||||
|
details.Should().NotBeNull();
|
||||||
|
details.Should().BeOfType<ProblemDetails>();
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -42,7 +79,24 @@ namespace server.tests.v1.unitTests
|
|||||||
[Fact]
|
[Fact]
|
||||||
public async Task GetSeasonByNumberAsync_RepoThrowsException_Returns500StatusCodeWithProblemDetails()
|
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<int>()), Times.Once());
|
||||||
|
|
||||||
|
response.Should().NotBeNull();
|
||||||
|
response.Should().BeOfType<ObjectResult>();
|
||||||
|
response.StatusCode.Should().Be((int)HttpStatusCode.InternalServerError);
|
||||||
|
|
||||||
|
details.Should().NotBeNull();
|
||||||
|
details.Should().BeOfType<ProblemDetails>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user