Added unit tests
This commit is contained in:
parent
b30304a291
commit
1ea95d81d1
6 changed files with 117 additions and 3 deletions
|
@ -15,6 +15,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{015E0E24-C
|
||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GitHubActionsDemo.Api.Tests", "test\GitHubActionsDemo.Api.Tests\GitHubActionsDemo.Api.Tests.csproj", "{AA6299AC-C10C-49CD-89A3-33122123AE7A}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GitHubActionsDemo.Api.Tests", "test\GitHubActionsDemo.Api.Tests\GitHubActionsDemo.Api.Tests.csproj", "{AA6299AC-C10C-49CD-89A3-33122123AE7A}"
|
||||||
EndProject
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GitHubActionsDemo.Service.Tests", "test\GitHubActionsDemo.Service.Tests\GitHubActionsDemo.Service.Tests.csproj", "{957CACE1-E456-48DA-84A7-02A81B9F0371}"
|
||||||
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
Debug|Any CPU = Debug|Any CPU
|
Debug|Any CPU = Debug|Any CPU
|
||||||
|
@ -40,11 +42,16 @@ Global
|
||||||
{AA6299AC-C10C-49CD-89A3-33122123AE7A}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{AA6299AC-C10C-49CD-89A3-33122123AE7A}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
{AA6299AC-C10C-49CD-89A3-33122123AE7A}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{AA6299AC-C10C-49CD-89A3-33122123AE7A}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
{AA6299AC-C10C-49CD-89A3-33122123AE7A}.Release|Any CPU.Build.0 = Release|Any CPU
|
{AA6299AC-C10C-49CD-89A3-33122123AE7A}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{957CACE1-E456-48DA-84A7-02A81B9F0371}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{957CACE1-E456-48DA-84A7-02A81B9F0371}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{957CACE1-E456-48DA-84A7-02A81B9F0371}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{957CACE1-E456-48DA-84A7-02A81B9F0371}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(NestedProjects) = preSolution
|
GlobalSection(NestedProjects) = preSolution
|
||||||
{E3C34F79-B74E-44A4-99F0-B0B3C2D7CCB2} = {53A44CCA-C5C6-4D98-91B8-D331D68BF7B0}
|
{E3C34F79-B74E-44A4-99F0-B0B3C2D7CCB2} = {53A44CCA-C5C6-4D98-91B8-D331D68BF7B0}
|
||||||
{F6807FFB-3C0D-4F84-8803-8ECAFD72EF76} = {53A44CCA-C5C6-4D98-91B8-D331D68BF7B0}
|
{F6807FFB-3C0D-4F84-8803-8ECAFD72EF76} = {53A44CCA-C5C6-4D98-91B8-D331D68BF7B0}
|
||||||
{339301B3-E9BD-45D1-B6A9-94F41367DF16} = {53A44CCA-C5C6-4D98-91B8-D331D68BF7B0}
|
{339301B3-E9BD-45D1-B6A9-94F41367DF16} = {53A44CCA-C5C6-4D98-91B8-D331D68BF7B0}
|
||||||
{AA6299AC-C10C-49CD-89A3-33122123AE7A} = {015E0E24-CD73-48C7-8565-5A566F5DAB87}
|
{AA6299AC-C10C-49CD-89A3-33122123AE7A} = {015E0E24-CD73-48C7-8565-5A566F5DAB87}
|
||||||
|
{957CACE1-E456-48DA-84A7-02A81B9F0371} = {015E0E24-CD73-48C7-8565-5A566F5DAB87}
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
EndGlobal
|
EndGlobal
|
||||||
|
|
|
@ -6,8 +6,6 @@ using GitHubActionsDemo.Api.Models;
|
||||||
using GitHubActionsDemo.Service;
|
using GitHubActionsDemo.Service;
|
||||||
using GitHubActionsDemo.Service.Models;
|
using GitHubActionsDemo.Service.Models;
|
||||||
using Microsoft.AspNetCore.Http.HttpResults;
|
using Microsoft.AspNetCore.Http.HttpResults;
|
||||||
using Moq;
|
|
||||||
using Shouldly;
|
|
||||||
|
|
||||||
namespace GitHubActionsDemo.Api.Tests;
|
namespace GitHubActionsDemo.Api.Tests;
|
||||||
|
|
||||||
|
@ -46,6 +44,7 @@ public class AuthorsControllerTests
|
||||||
.ReturnsAsync(new ValidationResult(failures));
|
.ReturnsAsync(new ValidationResult(failures));
|
||||||
|
|
||||||
var result = await _sut.AddAuthorAsync(request);
|
var result = await _sut.AddAuthorAsync(request);
|
||||||
|
result.ShouldBeOfType<ProblemHttpResult>();
|
||||||
var problem = result as ProblemHttpResult;
|
var problem = result as ProblemHttpResult;
|
||||||
problem.ShouldNotBeNull();
|
problem.ShouldNotBeNull();
|
||||||
}
|
}
|
||||||
|
@ -71,10 +70,13 @@ public class AuthorsControllerTests
|
||||||
.ReturnsAsync(new OneOf.Types.Success<Author>(mockAuthor));
|
.ReturnsAsync(new OneOf.Types.Success<Author>(mockAuthor));
|
||||||
|
|
||||||
var result = await _sut.AddAuthorAsync(request);
|
var result = await _sut.AddAuthorAsync(request);
|
||||||
|
result.ShouldBeOfType<Ok<AuthorResponse>>();
|
||||||
var successValue = result as Ok<AuthorResponse>;
|
var successValue = result as Ok<AuthorResponse>;
|
||||||
successValue.ShouldNotBeNull();
|
successValue.ShouldNotBeNull();
|
||||||
successValue.StatusCode.ShouldBe((int)HttpStatusCode.OK);
|
successValue.StatusCode.ShouldBe((int)HttpStatusCode.OK);
|
||||||
successValue.Value.FirstName.ShouldBe(request.FirstName);
|
successValue.Value.FirstName.ShouldBe(request.FirstName);
|
||||||
successValue.Value.LastName.ShouldBe(request.LastName);
|
successValue.Value.LastName.ShouldBe(request.LastName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: Note in a production application there would be a complete set of unit tests here.
|
||||||
}
|
}
|
|
@ -1 +1,3 @@
|
||||||
global using Xunit;
|
global using Xunit;
|
||||||
|
global using Moq;
|
||||||
|
global using Shouldly;
|
|
@ -0,0 +1,31 @@
|
||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>net7.0</TargetFramework>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
|
||||||
|
<IsPackable>false</IsPackable>
|
||||||
|
<IsTestProject>true</IsTestProject>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
|
||||||
|
<PackageReference Include="Moq" Version="4.18.4" />
|
||||||
|
<PackageReference Include="Shouldly" Version="4.2.1" />
|
||||||
|
<PackageReference Include="xunit" Version="2.4.2" />
|
||||||
|
<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.2.0">
|
||||||
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
|
<PrivateAssets>all</PrivateAssets>
|
||||||
|
</PackageReference>
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\..\src\GitHubActionsDemo.Service\GitHubActionsDemo.Service.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
69
test/GitHubActionsDemo.Service.Tests/LibraryServiceTests.cs
Normal file
69
test/GitHubActionsDemo.Service.Tests/LibraryServiceTests.cs
Normal file
|
@ -0,0 +1,69 @@
|
||||||
|
using System;
|
||||||
|
using GitHubActionsDemo.Persistance;
|
||||||
|
using GitHubActionsDemo.Persistance.Models;
|
||||||
|
using GitHubActionsDemo.Service.Models;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
|
namespace GitHubActionsDemo.Service.Tests;
|
||||||
|
|
||||||
|
public class LibraryServiceTests
|
||||||
|
{
|
||||||
|
private readonly Mock<ILibraryRespository> _libraryRespository;
|
||||||
|
private readonly Mock<ILogger<LibraryService>> _logger;
|
||||||
|
private readonly LibraryService _sut;
|
||||||
|
|
||||||
|
public LibraryServiceTests()
|
||||||
|
{
|
||||||
|
_libraryRespository = new Mock<ILibraryRespository>();
|
||||||
|
_logger = new Mock<ILogger<LibraryService>>();
|
||||||
|
_sut = new LibraryService(_libraryRespository.Object, _logger.Object);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Given_new_author_should_return_author()
|
||||||
|
{
|
||||||
|
var newAuthor = new NewAuthor("Joe", "Bloggs");
|
||||||
|
|
||||||
|
var mockAuthorDb = new AuthorDb
|
||||||
|
{
|
||||||
|
AuthorId = 1,
|
||||||
|
FirstName = newAuthor.FirstName,
|
||||||
|
LastName = newAuthor.LastName,
|
||||||
|
DateCreated = DateTime.UtcNow,
|
||||||
|
DateModified = DateTime.UtcNow
|
||||||
|
};
|
||||||
|
|
||||||
|
_libraryRespository.Setup(x => x.AddAuthorAsync(It.IsAny<NewAuthorDb>())).ReturnsAsync(mockAuthorDb.AuthorId);
|
||||||
|
_libraryRespository.Setup(x => x.GetAuthorAsync(1)).ReturnsAsync(mockAuthorDb);
|
||||||
|
|
||||||
|
var result = await _sut.AddAuthorAsync(newAuthor);
|
||||||
|
result.IsT0.ShouldBeTrue();
|
||||||
|
result.AsT0.Value.FirstName.ShouldBe(mockAuthorDb.FirstName);
|
||||||
|
result.AsT0.Value.LastName.ShouldBe(mockAuthorDb.LastName);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Given_new_book_should_return_book()
|
||||||
|
{
|
||||||
|
var newBook = new NewBook("Once Upon A Time", 1, "1234", DateTime.UtcNow);
|
||||||
|
|
||||||
|
var mockBookDb = new BookDb
|
||||||
|
{
|
||||||
|
BookId = 1,
|
||||||
|
Title = newBook.Title,
|
||||||
|
Author = new AuthorDb { AuthorId = 1, FirstName = "Joe", LastName = "Bloggs" },
|
||||||
|
Isbn = newBook.Isbn,
|
||||||
|
DatePublished = newBook.DatePublished,
|
||||||
|
DateCreated = DateTime.UtcNow,
|
||||||
|
DateModified = DateTime.UtcNow
|
||||||
|
};
|
||||||
|
|
||||||
|
_libraryRespository.Setup(x => x.AddBookAsync(It.IsAny<NewBookDb>())).ReturnsAsync(mockBookDb.BookId);
|
||||||
|
_libraryRespository.Setup(x => x.GetBookAsync(1)).ReturnsAsync(mockBookDb);
|
||||||
|
|
||||||
|
var result = await _sut.AddBookAsync(newBook);
|
||||||
|
result.IsT0.ShouldBeTrue();
|
||||||
|
result.AsT0.Value.Title.ShouldBe(newBook.Title);
|
||||||
|
result.AsT0.Value.Isbn.ShouldBe(newBook.Isbn);
|
||||||
|
}
|
||||||
|
}
|
3
test/GitHubActionsDemo.Service.Tests/Usings.cs
Normal file
3
test/GitHubActionsDemo.Service.Tests/Usings.cs
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
global using Xunit;
|
||||||
|
global using Moq;
|
||||||
|
global using Shouldly;
|
Loading…
Reference in a new issue