more testing

This commit is contained in:
2024-10-22 20:52:10 +11:00
parent 3a26c007d2
commit 1cd75cf481
13 changed files with 259 additions and 21 deletions

View File

@@ -0,0 +1,63 @@
using Microsoft.EntityFrameworkCore;
using UserManager.Application.Features.Users.Requests;
using UserManager.Application.Features.Users.Responses;
using UserManager.Application.Features.Users.Services;
using UserManager.Domain.Entities;
using UserManager.Infrastructure;
namespace UserManager.Application.IntegrationTests.Features.Users
{
[TestClass]
public class UserServiceIntegrationTests
{
private UserManagerContext _context = null!;
[TestInitialize]
public void Initialize()
{
DbContextOptions<UserManagerContext> options = new DbContextOptionsBuilder<UserManagerContext>()
.UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
.Options;
_context = new UserManagerContext(options);
}
[TestCleanup]
public void Cleanup()
{
_context.Dispose();
_context = null!;
}
[TestMethod]
public async Task CreateUserAsync_ShouldAddUserToDatabase_WhenCreateUserCalled()
{
// Arrange
var request = new CreateUserRequestDto()
{
FirstName = "John",
LastName = "Doe",
};
UnitOfWork unitOfWork = new(_context);
UserService userService = new(unitOfWork);
// Act
CreateUserResponseDto responseDto = await userService.CreateUserAsync(request);
// Assert
User user = _context.Users.First();
Assert.AreEqual(1, _context.Users.Count());
Assert.AreEqual("John", user.FirstName);
Assert.AreEqual("John", responseDto.FirstName);
Assert.AreEqual(1, user.UserId);
Assert.AreEqual(1, responseDto.UserId);
Assert.IsTrue(user.CreateAtUtc > DateTime.UtcNow.AddSeconds(-1));
Assert.IsTrue(user.CreateAtUtc < DateTime.UtcNow.AddSeconds(1));
Assert.IsNull(user.UpdatedAtUtc);
Assert.IsNull(user.DeletedAtUtc);
Assert.IsFalse(user.IsDeleted);
}
}
}

View File

@@ -0,0 +1,11 @@
namespace UserManager.Application.IntegrationTests
{
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
}
}
}

View File

@@ -0,0 +1,28 @@
<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="Microsoft.EntityFrameworkCore.InMemory" Version="8.0.10" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.1.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.1.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\UserManager.Infrastructure\UserManager.Infrastructure.csproj" />
</ItemGroup>
<ItemGroup>
<Using Include="Microsoft.VisualStudio.TestTools.UnitTesting" />
</ItemGroup>
</Project>