From c002e7ac1ee1a39af6cd7271485cc0827bd30756 Mon Sep 17 00:00:00 2001 From: Lars Unruh Date: Wed, 14 Jun 2023 10:32:57 +0200 Subject: [PATCH] add unit tests --- AB-API.sln | 15 ++++-- AB.Tests/AB.Tests.csproj | 31 +++++++++++ AB.Tests/CustomerTests.cs | 105 ++++++++++++++++++++++++++++++++++++++ AB.Tests/Usings.cs | 3 ++ 4 files changed, 151 insertions(+), 3 deletions(-) create mode 100644 AB.Tests/AB.Tests.csproj create mode 100644 AB.Tests/CustomerTests.cs create mode 100644 AB.Tests/Usings.cs diff --git a/AB-API.sln b/AB-API.sln index 1797ac3..9bac6bb 100644 --- a/AB-API.sln +++ b/AB-API.sln @@ -11,13 +11,17 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AB.Services", "AB.Services\ EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AB.Contracts", "AB.Contracts\AB.Contracts.csproj", "{9319695E-2237-49E2-80CD-761F53364421}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AB.Services.Abstractions", "AB.Services.Abstractions\AB.Services.Abstractions.csproj", "{4E3D1E97-AD83-4F66-825E-105441AF4E15}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AB.Services.Abstractions", "AB.Services.Abstractions\AB.Services.Abstractions.csproj", "{4E3D1E97-AD83-4F66-825E-105441AF4E15}" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Infrastructure", "Infrastructure", "{AD8DD52C-57EC-455A-9A8D-E50009FCE608}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AB.Persistence", "AB.Persistence\AB.Persistence.csproj", "{BF2F36D4-6EF4-43AB-840F-0336EB1723EC}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AB.Persistence", "AB.Persistence\AB.Persistence.csproj", "{BF2F36D4-6EF4-43AB-840F-0336EB1723EC}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AB.API", "AB.API\AB.API.csproj", "{7595A349-8990-467D-8122-8E79931359DC}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AB.API", "AB.API\AB.API.csproj", "{7595A349-8990-467D-8122-8E79931359DC}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{82B06293-18B6-44FF-9D74-18DE555BE86A}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AB.Tests", "AB.Tests\AB.Tests.csproj", "{8FF5BC04-5978-4240-BD91-A65524C35681}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -53,6 +57,10 @@ Global {7595A349-8990-467D-8122-8E79931359DC}.Debug|Any CPU.Build.0 = Debug|Any CPU {7595A349-8990-467D-8122-8E79931359DC}.Release|Any CPU.ActiveCfg = Release|Any CPU {7595A349-8990-467D-8122-8E79931359DC}.Release|Any CPU.Build.0 = Release|Any CPU + {8FF5BC04-5978-4240-BD91-A65524C35681}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {8FF5BC04-5978-4240-BD91-A65524C35681}.Debug|Any CPU.Build.0 = Debug|Any CPU + {8FF5BC04-5978-4240-BD91-A65524C35681}.Release|Any CPU.ActiveCfg = Release|Any CPU + {8FF5BC04-5978-4240-BD91-A65524C35681}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -60,6 +68,7 @@ Global GlobalSection(NestedProjects) = preSolution {BF2F36D4-6EF4-43AB-840F-0336EB1723EC} = {AD8DD52C-57EC-455A-9A8D-E50009FCE608} {7595A349-8990-467D-8122-8E79931359DC} = {AD8DD52C-57EC-455A-9A8D-E50009FCE608} + {8FF5BC04-5978-4240-BD91-A65524C35681} = {82B06293-18B6-44FF-9D74-18DE555BE86A} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {C7D1722E-5C50-4F16-BA7D-79977DD1D621} diff --git a/AB.Tests/AB.Tests.csproj b/AB.Tests/AB.Tests.csproj new file mode 100644 index 0000000..077e481 --- /dev/null +++ b/AB.Tests/AB.Tests.csproj @@ -0,0 +1,31 @@ + + + + net6.0 + enable + enable + + false + true + + + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + + + diff --git a/AB.Tests/CustomerTests.cs b/AB.Tests/CustomerTests.cs new file mode 100644 index 0000000..5bd8bb5 --- /dev/null +++ b/AB.Tests/CustomerTests.cs @@ -0,0 +1,105 @@ +using AB.Contracts; +using AB.Domain.Entities; +using AB.Domain.Exceptions; +using AB.Domain.Repositories; +using AB.Services; + +namespace AB.Tests +{ + public class CustomerTests + { + + private readonly Mock _unitOfWorkMock = new Mock(); + + private readonly Mock _customerRepositoryMock = new Mock(); + + [Fact] + public async void TestCreateCustomer_Sucess() + { + var customerService = new CustomerService(_customerRepositoryMock.Object, _unitOfWorkMock.Object); + + var customerForCreationDto = new CustomerForCreationDto + { + Salutaion = "Firma", + Name1 = "Arik Meyer", + Email = "test@test.de", + PhoneNumber = "1234567890", + Iban = "DE12345890", + }; + + var customerDto = await customerService.CreateAsync(customerForCreationDto); + + customerDto.Salutaion.Should().Be(customerForCreationDto.Salutaion); + customerDto.Name1.Should().Be(customerForCreationDto.Name1); + customerDto.Email.Should().Be(customerForCreationDto.Email); + customerDto.PhoneNumber.Should().Be(customerForCreationDto.PhoneNumber); + customerDto.Iban.Should().Be(customerForCreationDto.Iban); + } + + [Fact] + private async void TestGetCustomers_Sucess() + { + CancellationToken cancellationToken = default; + + var customerList = new List + { + new Customer + { + CustomerId = Guid.NewGuid(), + Salutation = "Herr", + }, + new Customer + { + CustomerId = Guid.NewGuid(), + Name1 = "Peter Sprudel" + } + + }; + + _customerRepositoryMock.Setup(repo => repo.GetAllAsync(cancellationToken)).ReturnsAsync(customerList); + + var customerService = new CustomerService(_customerRepositoryMock.Object, _unitOfWorkMock.Object); + + var dtoList = await customerService.GetAllAsync(cancellationToken); + + dtoList.Should().NotBeNullOrEmpty(); + + dtoList.ElementAt(0).Salutaion.Should().Be(customerList[0].Salutation); + dtoList.ElementAt(0).Id.Should().Be(customerList[0].CustomerId); + + dtoList.ElementAt(1).Name1.Should().Be(customerList[1].Name1); + dtoList.ElementAt(1).Id.Should().Be(customerList[1].CustomerId); + } + + [Fact] + private async void TestGetCustomerById_Exception() + { + var id = Guid.NewGuid(); + CancellationToken cancellationToken = default; + _customerRepositoryMock.Setup(repo => repo.GetByIdAsync(id, cancellationToken)).ThrowsAsync(new BusinessPartnerNotFoundException(id)); + + var customerService = new CustomerService(_customerRepositoryMock.Object, _unitOfWorkMock.Object); + + var exec = () => customerService.GetByIdAsync(id, cancellationToken); + + await exec.Should().ThrowExactlyAsync(); + + } + + [Fact] + private async void TestGetCustomerById_Sucess() + { + var id = Guid.NewGuid(); + CancellationToken cancellationToken = default; + var customer = new Customer { CustomerId = id }; + _customerRepositoryMock.Setup(repo => repo.GetByIdAsync(id, cancellationToken)).ReturnsAsync(customer); + + var customerService = new CustomerService(_customerRepositoryMock.Object, _unitOfWorkMock.Object); + + var dto = await customerService.GetByIdAsync(id, cancellationToken); + + dto.Should().NotBeNull(); + dto.Id.Should().Be(id); + } + } +} \ No newline at end of file diff --git a/AB.Tests/Usings.cs b/AB.Tests/Usings.cs new file mode 100644 index 0000000..d6558d2 --- /dev/null +++ b/AB.Tests/Usings.cs @@ -0,0 +1,3 @@ +global using Xunit; +global using FluentAssertions; +global using Moq; \ No newline at end of file