added test project and put integration and unit tests under single test project
This commit is contained in:
@@ -15,7 +15,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
|
|||||||
.editorconfig = .editorconfig
|
.editorconfig = .editorconfig
|
||||||
EndProjectSection
|
EndProjectSection
|
||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "server.integrationTests", "server.integrationTests\server.integrationTests.csproj", "{88C22EC9-3069-41AA-99EC-5503385A3633}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "server.tests", "server.tests\server.tests.csproj", "{8B86105B-F6F4-49A9-B384-C2823F693F38}"
|
||||||
EndProject
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
@@ -33,10 +33,10 @@ Global
|
|||||||
{1EF6EB3C-FCA0-4FF6-8A99-373AFAC6E4EC}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{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.ActiveCfg = Release|Any CPU
|
||||||
{1EF6EB3C-FCA0-4FF6-8A99-373AFAC6E4EC}.Release|Any CPU.Build.0 = Release|Any CPU
|
{1EF6EB3C-FCA0-4FF6-8A99-373AFAC6E4EC}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
{88C22EC9-3069-41AA-99EC-5503385A3633}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
{8B86105B-F6F4-49A9-B384-C2823F693F38}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
{88C22EC9-3069-41AA-99EC-5503385A3633}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{8B86105B-F6F4-49A9-B384-C2823F693F38}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
{88C22EC9-3069-41AA-99EC-5503385A3633}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{8B86105B-F6F4-49A9-B384-C2823F693F38}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
{88C22EC9-3069-41AA-99EC-5503385A3633}.Release|Any CPU.Build.0 = Release|Any CPU
|
{8B86105B-F6F4-49A9-B384-C2823F693F38}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
|
|||||||
@@ -1,38 +0,0 @@
|
|||||||
using Microsoft.AspNetCore.Mvc.Testing;
|
|
||||||
using server.Models;
|
|
||||||
using System.Net;
|
|
||||||
using System.Text.Json;
|
|
||||||
|
|
||||||
namespace server.integrationTests
|
|
||||||
{
|
|
||||||
[TestClass]
|
|
||||||
public class CharacterEndpointsTests
|
|
||||||
{
|
|
||||||
private readonly HttpClient _httpClient;
|
|
||||||
private readonly JsonSerializerOptions _serializerOptions;
|
|
||||||
public TestContext? TestContext { get; set; }
|
|
||||||
|
|
||||||
public CharacterEndpointsTests()
|
|
||||||
{
|
|
||||||
var webAppFactory = new WebApplicationFactory<Program>();
|
|
||||||
|
|
||||||
_httpClient = webAppFactory.CreateDefaultClient();
|
|
||||||
|
|
||||||
_serializerOptions = new JsonSerializerOptions
|
|
||||||
{
|
|
||||||
PropertyNameCaseInsensitive = true
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
[TestMethod]
|
|
||||||
public async Task GetCharacters_AllCharacters_Returns200StatusWithCharacters()
|
|
||||||
{
|
|
||||||
var response = await _httpClient.GetAsync("/api/characters");
|
|
||||||
var data = await response.Content.ReadAsStringAsync();
|
|
||||||
var characters = JsonSerializer.Deserialize<List<Character>>(data, _serializerOptions);
|
|
||||||
|
|
||||||
Assert.IsNotNull(characters);
|
|
||||||
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
global using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
||||||
@@ -1,23 +0,0 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
|
||||||
|
|
||||||
<PropertyGroup>
|
|
||||||
<TargetFramework>net6.0</TargetFramework>
|
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
|
||||||
<Nullable>enable</Nullable>
|
|
||||||
|
|
||||||
<IsPackable>false</IsPackable>
|
|
||||||
</PropertyGroup>
|
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="6.0.6" />
|
|
||||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
|
|
||||||
<PackageReference Include="MSTest.TestAdapter" Version="2.2.8" />
|
|
||||||
<PackageReference Include="MSTest.TestFramework" Version="2.2.8" />
|
|
||||||
<PackageReference Include="coverlet.collector" Version="3.1.2" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<ProjectReference Include="..\server\server.csproj" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
</Project>
|
|
||||||
@@ -1,124 +0,0 @@
|
|||||||
using Microsoft.AspNetCore.Mvc;
|
|
||||||
using Moq;
|
|
||||||
using server.Controllers.v1;
|
|
||||||
using server.Models;
|
|
||||||
using server.Persistence.Repositories;
|
|
||||||
using server.tests.Mocks;
|
|
||||||
using System.Net;
|
|
||||||
using System.Text.Json;
|
|
||||||
using Xunit.Abstractions;
|
|
||||||
|
|
||||||
namespace tests.controllers
|
|
||||||
{
|
|
||||||
public class CharactersControllerTests
|
|
||||||
{
|
|
||||||
private readonly Mock<ICharacterRepository> _mockRepo;
|
|
||||||
private readonly CharactersController _controller;
|
|
||||||
private readonly ITestOutputHelper _output;
|
|
||||||
|
|
||||||
public CharactersControllerTests(ITestOutputHelper output)
|
|
||||||
{
|
|
||||||
_mockRepo = new Mock<ICharacterRepository>();
|
|
||||||
_controller = new CharactersController(_mockRepo.Object);
|
|
||||||
_output = output;
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public async Task GetCharactersAsync_NoFilterParamsWithCharacters_Returns200StatusWithCharactersCollection()
|
|
||||||
{
|
|
||||||
var filter = new CharacterFilter();
|
|
||||||
|
|
||||||
var character = new Character
|
|
||||||
{
|
|
||||||
Id = "62b7d5506c1b407771829926",
|
|
||||||
FirstName = "Jason",
|
|
||||||
LastName = "Gideon",
|
|
||||||
ActorFirstName = "Mandy",
|
|
||||||
ActorLastName = "Patinkin",
|
|
||||||
Seasons = new int[] {1,2,3,10,15},
|
|
||||||
FirstEpisode = "Extreme Aggressor",
|
|
||||||
LastEpisode = "In Name and Blood",
|
|
||||||
Image = "https://criminalmindsapi.stevanfreeborn.com/characters/jason-gideon.png",
|
|
||||||
Bio = "Jason Gideon was a criminal profiler, formerly the Senior Supervisory Special Agent of the FBI's Behavioral Analysis Unit. At the beginning of Season Three, Gideon abruptly retired from the BAU due to emotional issues brought on by the murder of his girlfriend. His position is now held by his former partner and best friend David Rossi, who has held it to this day. In the Season Ten episode \"Nelson's Sparrow\", he was murdered by Donnie Mallick.",
|
|
||||||
};
|
|
||||||
|
|
||||||
var characters = new List<Character>
|
|
||||||
{
|
|
||||||
character
|
|
||||||
};
|
|
||||||
|
|
||||||
_mockRepo
|
|
||||||
.Setup(repo => repo.GetCharactersAsync(filter))
|
|
||||||
.ReturnsAsync(characters);
|
|
||||||
|
|
||||||
var result = await _controller.GetCharactersAsync(filter) as ObjectResult;
|
|
||||||
var data = result?.Value as List<Character>;
|
|
||||||
|
|
||||||
_mockRepo.Verify(repo => repo.GetCharactersAsync(It.IsAny<CharacterFilter>()), Times.Once());
|
|
||||||
Assert.IsType<OkObjectResult>(result);
|
|
||||||
Assert.IsType<List<Character>>(data);
|
|
||||||
Assert.Equal(HttpStatusCode.OK, (HttpStatusCode)result.StatusCode);
|
|
||||||
Assert.Single(data);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public async Task GetCharactersAsync_RepoThrowsException_Returns500StatusWithProblemDetail()
|
|
||||||
{
|
|
||||||
var filter = new CharacterFilter();
|
|
||||||
|
|
||||||
_mockRepo
|
|
||||||
.Setup(repo => repo.GetCharactersAsync(filter))
|
|
||||||
.Throws(new Exception());
|
|
||||||
|
|
||||||
var result = await _controller.GetCharactersAsync(filter) as ObjectResult;
|
|
||||||
var data = result?.Value;
|
|
||||||
|
|
||||||
_mockRepo.Verify(repo => repo.GetCharactersAsync(It.IsAny<CharacterFilter>()), Times.Once());
|
|
||||||
Assert.IsType<ObjectResult>(result);
|
|
||||||
Assert.IsType<ProblemDetails>(data);
|
|
||||||
Assert.Equal(HttpStatusCode.InternalServerError, (HttpStatusCode)result.StatusCode);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public async Task GetCharacterByIdAsync_ValidCharacterId_Returns200StatusWithCharacter()
|
|
||||||
{
|
|
||||||
throw new NotImplementedException();
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public async Task GetCharacterByIdAsync_InvalidCharacterId_Returns400StatusWithValidationProblemDetail()
|
|
||||||
{
|
|
||||||
var characterId = "1";
|
|
||||||
|
|
||||||
_controller.ProblemDetailsFactory = new MockProblemDetailsFactory();
|
|
||||||
|
|
||||||
var result = await _controller.GetCharacterByIdAsync(characterId) as ObjectResult;
|
|
||||||
var data = result?.Value;
|
|
||||||
|
|
||||||
string json = JsonSerializer.Serialize(result);
|
|
||||||
|
|
||||||
_output.WriteLine(json);
|
|
||||||
|
|
||||||
// Assert.IsType<ObjectResult>(result);
|
|
||||||
// Assert.IsType<ValidationProblemDetails>();
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public async Task GetCharacterByIdAsync_RepoThrowsException_Returns500StatusWithProblemDetail()
|
|
||||||
{
|
|
||||||
var characterId = "62b7d5506c1b407771829938";
|
|
||||||
|
|
||||||
_mockRepo
|
|
||||||
.Setup(repo => repo.GetCharacterByIdAsync(characterId))
|
|
||||||
.Throws(new Exception());
|
|
||||||
|
|
||||||
var result = await _controller.GetCharacterByIdAsync(characterId) as ObjectResult;
|
|
||||||
var data = result?.Value;
|
|
||||||
|
|
||||||
_mockRepo.Verify(repo => repo.GetCharacterByIdAsync(It.IsAny<string>()), Times.Once());
|
|
||||||
Assert.IsType<ObjectResult>(result);
|
|
||||||
Assert.IsType<ProblemDetails>(data);
|
|
||||||
Assert.Equal(HttpStatusCode.InternalServerError, (HttpStatusCode)result.StatusCode);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
using FluentAssertions;
|
||||||
|
using Microsoft.AspNetCore.Mvc.Testing;
|
||||||
|
using System.Net;
|
||||||
|
|
||||||
|
namespace server.tests.integrationTests
|
||||||
|
{
|
||||||
|
public class CharactersControllerIntegrationTests
|
||||||
|
{
|
||||||
|
private readonly HttpClient _client;
|
||||||
|
|
||||||
|
public CharactersControllerIntegrationTests()
|
||||||
|
{
|
||||||
|
var webAppFactory = new WebApplicationFactory<Program>();
|
||||||
|
_client = webAppFactory.CreateDefaultClient();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task GetCharacters_RetrievesAllCharacters_Returns200StatusCodeWithCharacters()
|
||||||
|
{
|
||||||
|
var response = await _client.GetAsync("/api/characters");
|
||||||
|
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -9,9 +9,11 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.2.0" />
|
<PackageReference Include="FluentAssertions" Version="6.7.0" />
|
||||||
|
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="6.0.6" />
|
||||||
|
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
|
||||||
<PackageReference Include="xunit" Version="2.4.1" />
|
<PackageReference Include="xunit" Version="2.4.1" />
|
||||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
|
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
|
||||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
<PrivateAssets>all</PrivateAssets>
|
<PrivateAssets>all</PrivateAssets>
|
||||||
</PackageReference>
|
</PackageReference>
|
||||||
|
|||||||
@@ -0,0 +1,18 @@
|
|||||||
|
using Moq;
|
||||||
|
using server.Controllers.v1;
|
||||||
|
using server.Persistence.Repositories;
|
||||||
|
|
||||||
|
namespace server.tests.unitTests
|
||||||
|
{
|
||||||
|
public class CharacterControllerUnitTests
|
||||||
|
{
|
||||||
|
private readonly Mock<ICharacterRepository> _mockRepo;
|
||||||
|
private readonly CharactersController _controller;
|
||||||
|
|
||||||
|
public CharacterControllerUnitTests()
|
||||||
|
{
|
||||||
|
_mockRepo = new Mock<ICharacterRepository>();
|
||||||
|
_controller = new CharactersController(_mockRepo.Object);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -21,7 +21,7 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<InternalsVisibleTo Include="server.integrationTests" />
|
<InternalsVisibleTo Include="server.tests" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
Reference in New Issue
Block a user