begin writing tests for controllers
This commit is contained in:
@@ -8,3 +8,9 @@ dotnet_diagnostic.CS8604.severity = none
|
||||
|
||||
# CS1591: Missing XML comment for publicly visible type or member
|
||||
dotnet_diagnostic.CS1591.severity = none
|
||||
|
||||
# CS8629: Nullable value type may be null.
|
||||
dotnet_diagnostic.CS8629.severity = silent
|
||||
|
||||
# CS8602: Dereference of a possibly null reference.
|
||||
dotnet_diagnostic.CS8602.severity = silent
|
||||
|
||||
@@ -15,6 +15,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
|
||||
.editorconfig = .editorconfig
|
||||
EndProjectSection
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "tests", "tests\tests.csproj", "{B78134BA-1C1D-43CC-9B41-78C67B057AAC}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
@@ -31,6 +33,10 @@ Global
|
||||
{1EF6EB3C-FCA0-4FF6-8A99-373AFAC6E4EC}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{1EF6EB3C-FCA0-4FF6-8A99-373AFAC6E4EC}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{1EF6EB3C-FCA0-4FF6-8A99-373AFAC6E4EC}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{B78134BA-1C1D-43CC-9B41-78C67B057AAC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{B78134BA-1C1D-43CC-9B41-78C67B057AAC}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{B78134BA-1C1D-43CC-9B41-78C67B057AAC}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{B78134BA-1C1D-43CC-9B41-78C67B057AAC}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
||||
@@ -31,7 +31,7 @@ namespace server.Controllers.v1
|
||||
[ProducesResponseType(typeof(List<Character>), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status500InternalServerError)]
|
||||
public async Task<ActionResult<List<Character>>> GetCharactersAsync([FromQuery] CharacterFilter? filter)
|
||||
public async Task<IActionResult> GetCharactersAsync([FromQuery] CharacterFilter? filter)
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -60,7 +60,7 @@ namespace server.Controllers.v1
|
||||
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)]
|
||||
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status500InternalServerError)]
|
||||
public async Task<ActionResult<Character>> GetCharacterByIdAsync(string id)
|
||||
public async Task<IActionResult> GetCharacterByIdAsync(string id)
|
||||
{
|
||||
|
||||
if (!ObjectId.TryParse(id, out _))
|
||||
|
||||
@@ -30,7 +30,7 @@ namespace server.Controllers.v1
|
||||
[ProducesResponseType(typeof(List<Episode>), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status500InternalServerError)]
|
||||
public async Task<ActionResult<List<Episode>>> GetEpisodesAsync([FromQuery] EpisodeFilter? filter)
|
||||
public async Task<IActionResult> GetEpisodesAsync([FromQuery] EpisodeFilter? filter)
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -59,7 +59,7 @@ namespace server.Controllers.v1
|
||||
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)]
|
||||
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status500InternalServerError)]
|
||||
public async Task<ActionResult<Episode>> GetEpisodeByNumberAsync(int number)
|
||||
public async Task<IActionResult> GetEpisodeByNumberAsync(int number)
|
||||
{
|
||||
try
|
||||
{
|
||||
|
||||
@@ -32,7 +32,7 @@ namespace server.Controllers.v1
|
||||
[ProducesResponseType(typeof(List<Quote>), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status500InternalServerError)]
|
||||
public async Task<ActionResult<List<Quote>>> GetQuotesAsync([FromQuery] QuoteFilter? filter)
|
||||
public async Task<IActionResult> GetQuotesAsync([FromQuery] QuoteFilter? filter)
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -61,7 +61,7 @@ namespace server.Controllers.v1
|
||||
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)]
|
||||
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status500InternalServerError)]
|
||||
public async Task<ActionResult<Quote>> GetQuoteByIdAsync(string id)
|
||||
public async Task<IActionResult> GetQuoteByIdAsync(string id)
|
||||
{
|
||||
|
||||
if (!ObjectId.TryParse(id, out _))
|
||||
|
||||
@@ -27,7 +27,7 @@ namespace server.Controllers.v1
|
||||
[HttpGet]
|
||||
[ProducesResponseType(typeof(List<Season>), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status500InternalServerError)]
|
||||
public async Task<ActionResult<List<Season>>> GetSeasonsAsync()
|
||||
public async Task<IActionResult> GetSeasonsAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -53,7 +53,7 @@ namespace server.Controllers.v1
|
||||
[ProducesResponseType(typeof(Season), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)]
|
||||
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status500InternalServerError)]
|
||||
public async Task<ActionResult<Season>> GetSeasonByNumberAsync(int number)
|
||||
public async Task<IActionResult> GetSeasonByNumberAsync(int number)
|
||||
{
|
||||
try
|
||||
{
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="5.0.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer" Version="5.0.0" />
|
||||
<PackageReference Include="MongoDB.Driver" Version="2.16.0" />
|
||||
<PackageReference Include="Moq" Version="4.18.1" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.3.1" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
global using Xunit;
|
||||
@@ -0,0 +1,40 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Moq;
|
||||
using server.Controllers.v1;
|
||||
using server.Models;
|
||||
using server.Persistence.Repositories;
|
||||
using System.Net;
|
||||
|
||||
namespace tests.controllers
|
||||
{
|
||||
public class CharactersControllerTests
|
||||
{
|
||||
private readonly Mock<ICharacterRepository> _mockRepo;
|
||||
private readonly CharactersController _controller;
|
||||
|
||||
public CharactersControllerTests()
|
||||
{
|
||||
_mockRepo = new Mock<ICharacterRepository>();
|
||||
_controller = new CharactersController(_mockRepo.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetCharactersAsync_NoFilterNoCharacters_Returns200StatusWithEmptyCollection()
|
||||
{
|
||||
var filter = new CharacterFilter();
|
||||
|
||||
_mockRepo
|
||||
.Setup(repo => repo.GetCharactersAsync(filter))
|
||||
.ReturnsAsync( new List<Character>());
|
||||
|
||||
var result = await _controller.GetCharactersAsync(filter) as ObjectResult;
|
||||
var data = result?.Value as List<Character>;
|
||||
|
||||
_mockRepo.Verify(c => c.GetCharactersAsync(filter), Times.Once());
|
||||
Assert.IsType<OkObjectResult>(result);
|
||||
Assert.IsType<List<Character>>(data);
|
||||
Assert.Equal(HttpStatusCode.OK, (HttpStatusCode)result.StatusCode);
|
||||
Assert.Empty(data);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.2.0" />
|
||||
<PackageReference Include="xunit" Version="2.4.1" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="coverlet.collector" Version="3.1.2">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\server\server.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
Reference in New Issue
Block a user