From 1ea95d81d1a692cd5c06e563f146a91454674cfd Mon Sep 17 00:00:00 2001 From: Alex Hyett Date: Wed, 5 Jul 2023 12:15:11 +0100 Subject: [PATCH] Added unit tests --- GitHubActionsDemo.sln | 7 ++ .../AuthorsControllerTests.cs | 6 +- test/GitHubActionsDemo.Api.Tests/Usings.cs | 4 +- .../GitHubActionsDemo.Service.Tests.csproj | 31 +++++++++ .../LibraryServiceTests.cs | 69 +++++++++++++++++++ .../GitHubActionsDemo.Service.Tests/Usings.cs | 3 + 6 files changed, 117 insertions(+), 3 deletions(-) create mode 100644 test/GitHubActionsDemo.Service.Tests/GitHubActionsDemo.Service.Tests.csproj create mode 100644 test/GitHubActionsDemo.Service.Tests/LibraryServiceTests.cs create mode 100644 test/GitHubActionsDemo.Service.Tests/Usings.cs diff --git a/GitHubActionsDemo.sln b/GitHubActionsDemo.sln index 215bceb..8b770c8 100644 --- a/GitHubActionsDemo.sln +++ b/GitHubActionsDemo.sln @@ -15,6 +15,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{015E0E24-C EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GitHubActionsDemo.Api.Tests", "test\GitHubActionsDemo.Api.Tests\GitHubActionsDemo.Api.Tests.csproj", "{AA6299AC-C10C-49CD-89A3-33122123AE7A}" 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 GlobalSection(SolutionConfigurationPlatforms) = preSolution 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}.Release|Any CPU.ActiveCfg = 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 GlobalSection(NestedProjects) = preSolution {E3C34F79-B74E-44A4-99F0-B0B3C2D7CCB2} = {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} {AA6299AC-C10C-49CD-89A3-33122123AE7A} = {015E0E24-CD73-48C7-8565-5A566F5DAB87} + {957CACE1-E456-48DA-84A7-02A81B9F0371} = {015E0E24-CD73-48C7-8565-5A566F5DAB87} EndGlobalSection EndGlobal diff --git a/test/GitHubActionsDemo.Api.Tests/AuthorsControllerTests.cs b/test/GitHubActionsDemo.Api.Tests/AuthorsControllerTests.cs index 81b956c..e6f0102 100644 --- a/test/GitHubActionsDemo.Api.Tests/AuthorsControllerTests.cs +++ b/test/GitHubActionsDemo.Api.Tests/AuthorsControllerTests.cs @@ -6,8 +6,6 @@ using GitHubActionsDemo.Api.Models; using GitHubActionsDemo.Service; using GitHubActionsDemo.Service.Models; using Microsoft.AspNetCore.Http.HttpResults; -using Moq; -using Shouldly; namespace GitHubActionsDemo.Api.Tests; @@ -46,6 +44,7 @@ public class AuthorsControllerTests .ReturnsAsync(new ValidationResult(failures)); var result = await _sut.AddAuthorAsync(request); + result.ShouldBeOfType(); var problem = result as ProblemHttpResult; problem.ShouldNotBeNull(); } @@ -71,10 +70,13 @@ public class AuthorsControllerTests .ReturnsAsync(new OneOf.Types.Success(mockAuthor)); var result = await _sut.AddAuthorAsync(request); + result.ShouldBeOfType>(); var successValue = result as Ok; successValue.ShouldNotBeNull(); successValue.StatusCode.ShouldBe((int)HttpStatusCode.OK); successValue.Value.FirstName.ShouldBe(request.FirstName); successValue.Value.LastName.ShouldBe(request.LastName); } + + // TODO: Note in a production application there would be a complete set of unit tests here. } \ No newline at end of file diff --git a/test/GitHubActionsDemo.Api.Tests/Usings.cs b/test/GitHubActionsDemo.Api.Tests/Usings.cs index 8c927eb..d093222 100644 --- a/test/GitHubActionsDemo.Api.Tests/Usings.cs +++ b/test/GitHubActionsDemo.Api.Tests/Usings.cs @@ -1 +1,3 @@ -global using Xunit; \ No newline at end of file +global using Xunit; +global using Moq; +global using Shouldly; \ No newline at end of file diff --git a/test/GitHubActionsDemo.Service.Tests/GitHubActionsDemo.Service.Tests.csproj b/test/GitHubActionsDemo.Service.Tests/GitHubActionsDemo.Service.Tests.csproj new file mode 100644 index 0000000..c82ba3e --- /dev/null +++ b/test/GitHubActionsDemo.Service.Tests/GitHubActionsDemo.Service.Tests.csproj @@ -0,0 +1,31 @@ + + + + net7.0 + enable + enable + + false + true + + + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + + + diff --git a/test/GitHubActionsDemo.Service.Tests/LibraryServiceTests.cs b/test/GitHubActionsDemo.Service.Tests/LibraryServiceTests.cs new file mode 100644 index 0000000..67f777d --- /dev/null +++ b/test/GitHubActionsDemo.Service.Tests/LibraryServiceTests.cs @@ -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 _libraryRespository; + private readonly Mock> _logger; + private readonly LibraryService _sut; + + public LibraryServiceTests() + { + _libraryRespository = new Mock(); + _logger = new Mock>(); + _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())).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())).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); + } +} \ No newline at end of file diff --git a/test/GitHubActionsDemo.Service.Tests/Usings.cs b/test/GitHubActionsDemo.Service.Tests/Usings.cs new file mode 100644 index 0000000..d093222 --- /dev/null +++ b/test/GitHubActionsDemo.Service.Tests/Usings.cs @@ -0,0 +1,3 @@ +global using Xunit; +global using Moq; +global using Shouldly; \ No newline at end of file