add integration tests
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
<Project Sdk="Microsoft.VisualStudio.JavaScript.Sdk/0.5.26-alpha">
|
||||
<PropertyGroup Label="Globals">
|
||||
<ProjectGuid>8fc072ca-af79-4930-b9b1-139230dc3426</ProjectGuid>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup>
|
||||
<StartupCommand>set BROWSER=none&&npm start</StartupCommand>
|
||||
<JavaScriptTestRoot>src\</JavaScriptTestRoot>
|
||||
<JavaScriptTestFramework>Jest</JavaScriptTestFramework>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Label="Globals">
|
||||
<ProjectGuid>8fc072ca-af79-4930-b9b1-139230dc3426</ProjectGuid>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup>
|
||||
<StartupCommand>set BROWSER=none&&npm start</StartupCommand>
|
||||
<JavaScriptTestRoot>src\</JavaScriptTestRoot>
|
||||
<JavaScriptTestFramework>Jest</JavaScriptTestFramework>
|
||||
</PropertyGroup>
|
||||
</Project>
|
||||
@@ -15,7 +15,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
|
||||
.editorconfig = .editorconfig
|
||||
EndProjectSection
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "server.tests", "server.tests\server.tests.csproj", "{F47F6C5F-DB28-44D7-B9CF-6B927755B2D8}"
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "server.integrationTests", "server.integrationTests\server.integrationTests.csproj", "{88C22EC9-3069-41AA-99EC-5503385A3633}"
|
||||
EndProject
|
||||
Global
|
||||
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}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{1EF6EB3C-FCA0-4FF6-8A99-373AFAC6E4EC}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{F47F6C5F-DB28-44D7-B9CF-6B927755B2D8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{F47F6C5F-DB28-44D7-B9CF-6B927755B2D8}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{F47F6C5F-DB28-44D7-B9CF-6B927755B2D8}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{F47F6C5F-DB28-44D7-B9CF-6B927755B2D8}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{88C22EC9-3069-41AA-99EC-5503385A3633}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{88C22EC9-3069-41AA-99EC-5503385A3633}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{88C22EC9-3069-41AA-99EC-5503385A3633}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{88C22EC9-3069-41AA-99EC-5503385A3633}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
global using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
@@ -0,0 +1,23 @@
|
||||
<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>
|
||||
@@ -3,8 +3,10 @@ using Moq;
|
||||
using server.Controllers.v1;
|
||||
using server.Models;
|
||||
using server.Persistence.Repositories;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using server.tests.Mocks;
|
||||
using System.Net;
|
||||
using System.Text.Json;
|
||||
using Xunit.Abstractions;
|
||||
|
||||
namespace tests.controllers
|
||||
{
|
||||
@@ -12,11 +14,13 @@ namespace tests.controllers
|
||||
{
|
||||
private readonly Mock<ICharacterRepository> _mockRepo;
|
||||
private readonly CharactersController _controller;
|
||||
private readonly ITestOutputHelper _output;
|
||||
|
||||
public CharactersControllerTests()
|
||||
public CharactersControllerTests(ITestOutputHelper output)
|
||||
{
|
||||
_mockRepo = new Mock<ICharacterRepository>();
|
||||
_controller = new CharactersController(_mockRepo.Object);
|
||||
_output = output;
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -67,12 +71,54 @@ namespace tests.controllers
|
||||
.Throws(new Exception());
|
||||
|
||||
var result = await _controller.GetCharactersAsync(filter) as ObjectResult;
|
||||
var data = result.Value;
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+21
-17
@@ -1,23 +1,27 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<GenerateDocumentationFile>True</GenerateDocumentationFile>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<GenerateDocumentationFile>True</GenerateDocumentationFile>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Include="..\.editorconfig" Link=".editorconfig" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="..\.editorconfig" Link=".editorconfig" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AspNetCoreRateLimit" Version="4.0.2" />
|
||||
<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>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AspNetCoreRateLimit" Version="4.0.2" />
|
||||
<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>
|
||||
|
||||
<ItemGroup>
|
||||
<InternalsVisibleTo Include="server.integrationTests" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
Reference in New Issue
Block a user