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 options = new DbContextOptionsBuilder() .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); } } }