Adding Unit Test

This commit is contained in:
fa0xh1 2024-07-16 15:09:34 +07:00
parent 11c11a1309
commit 8ea964cd2d
7 changed files with 199 additions and 0 deletions

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,30 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="NUnit" Version="3.14.0" />
<PackageReference Include="NUnit.Analyzers" Version="3.9.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\core\MiniSkeletonAPI.Application\MiniSkeletonAPI.Application.csproj" />
</ItemGroup>
<ItemGroup>
<Using Include="NUnit.Framework" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,45 @@
using MiniSkeletonAPI.Application.Common.Behaviours;
using MiniSkeletonAPI.Application.Common.Interfaces;
using MiniSkeletonAPI.Application.TodoItems.Commands.CreateTodoItem;
using Microsoft.Extensions.Logging;
using Moq;
using NUnit.Framework;
namespace Application.UnitTests.Common.Behaviours;
public class RequestLoggerTests
{
private Mock<ILogger<CreateTodoItemCommand>> _logger = null!;
private Mock<IUser> _user = null!;
private Mock<IIdentityService> _identityService = null!;
[SetUp]
public void Setup()
{
_logger = new Mock<ILogger<CreateTodoItemCommand>>();
_user = new Mock<IUser>();
_identityService = new Mock<IIdentityService>();
}
[Test]
public async Task ShouldCallGetUserNameAsyncOnceIfAuthenticated()
{
_user.Setup(x => x.Id).Returns(Guid.NewGuid().ToString());
var requestLogger = new LoggingBehaviour<CreateTodoItemCommand>(_logger.Object, _user.Object, _identityService.Object);
await requestLogger.Process(new CreateTodoItemCommand { ListId = new Guid(), Title = "title" }, new CancellationToken());
_identityService.Verify(i => i.GetUserNameAsync(It.IsAny<string>()), Times.Once);
}
[Test]
public async Task ShouldNotCallGetUserNameAsyncOnceIfUnauthenticated()
{
var requestLogger = new LoggingBehaviour<CreateTodoItemCommand>(_logger.Object, _user.Object, _identityService.Object);
await requestLogger.Process(new CreateTodoItemCommand { ListId = new Guid(), Title = "title" }, new CancellationToken());
_identityService.Verify(i => i.GetUserNameAsync(It.IsAny<string>()), Times.Never);
}
}

View File

@ -0,0 +1,55 @@
using MiniSkeletonAPI.Application.Common.Exceptions;
using FluentAssertions;
using FluentValidation.Results;
using NUnit.Framework;
namespace Application.UnitTests.Common.Exceptions;
public class ValidationExceptionTests
{
[Test]
public void DefaultConstructorCreatesAnEmptyErrorDictionary()
{
var actual = new ValidationException().Errors;
actual.Keys.Should().BeEquivalentTo(Array.Empty<string>());
}
[Test]
public void SingleValidationFailureCreatesASingleElementErrorDictionary()
{
var failures = new List<ValidationFailure>
{
new ValidationFailure("Password", "must contain at least 8 characters"),
};
var actual = new ValidationException(failures).Errors;
actual.Keys.Should().BeEquivalentTo(new string[] { "Password" });
actual["Password"].Should().BeEquivalentTo(new string[] { "must contain at least 8 characters" });
}
[Test]
public void MulitpleValidationFailureForMultiplePropertiesCreatesAMultipleElementErrorDictionaryEachWithMultipleValues()
{
var failures = new List<ValidationFailure>
{
new ValidationFailure("Password", "must contain at least 8 characters"),
new ValidationFailure("Password", "must contain a digit"),
new ValidationFailure("Password", "must contain upper case letter"),
new ValidationFailure("Password", "must contain lower case letter"),
};
var actual = new ValidationException(failures).Errors;
actual.Keys.Should().BeEquivalentTo(new string[] { "Password" });
actual["Password"].Should().BeEquivalentTo(new string[]
{
"must contain lower case letter",
"must contain upper case letter",
"must contain at least 8 characters",
"must contain a digit",
});
}
}

View File

@ -0,0 +1,53 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using AutoMapper;
using MiniSkeletonAPI.Application.Common.Interfaces;
using MiniSkeletonAPI.Application.Common.Models;
using MiniSkeletonAPI.Application.TodoItems.Queries.GetTodoItemsWithPagination;
using MiniSkeletonAPI.Application.TodoLists.Queries.GetTodos;
using MiniSkeletonAPI.Domain.Entities;
using NUnit.Framework;
namespace Application.UnitTests.Common.Mappings;
public class MappingTests
{
private readonly IConfigurationProvider _configuration;
private readonly IMapper _mapper;
public MappingTests()
{
_configuration = new MapperConfiguration(config =>
config.AddMaps(Assembly.GetAssembly(typeof(IApplicationDbContext))));
_mapper = _configuration.CreateMapper();
}
[Test]
public void ShouldHaveValidConfiguration()
{
_configuration.AssertConfigurationIsValid();
}
[Test]
[TestCase(typeof(TodoList), typeof(TodoListDto))]
[TestCase(typeof(TodoItem), typeof(TodoItemDto))]
[TestCase(typeof(TodoList), typeof(LookupDto))]
[TestCase(typeof(TodoItem), typeof(LookupDto))]
[TestCase(typeof(TodoItem), typeof(TodoItemBriefDto))]
public void ShouldSupportMappingFromSourceToDestination(Type source, Type destination)
{
var instance = GetInstanceOf(source);
_mapper.Map(instance, source, destination);
}
private object GetInstanceOf(Type type)
{
if (type.GetConstructor(Type.EmptyTypes) != null)
return Activator.CreateInstance(type)!;
// Type without parameterless constructor
return RuntimeHelpers.GetUninitializedObject(type);
}
}

View File

@ -0,0 +1,16 @@
namespace Application.UnitTests
{
public class Tests
{
[SetUp]
public void Setup()
{
}
[Test]
public void Test1()
{
Assert.Pass();
}
}
}