clean-architecture-demo-use.../tests/UserManager.Application.IntegrationTests/Features/Users/UserServiceIntegrationTests.cs
2024-10-22 20:52:10 +11:00

64 lines
2.1 KiB
C#

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);
}
}
}