clean-architecture-demo-use.../tests/UserManager.Application.Tests/Features/Users/UserServiceUnitTests.cs

38 lines
1.3 KiB
C#
Raw Permalink Normal View History

2024-10-22 20:52:10 +11:00
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using UserManager.Application.Features.Users.Requests;
using UserManager.Application.Interfaces;
using UserManager.Application.Features.Users.Services;
using UserManager.Application.Features.Users.Responses;
using UserManager.Domain.Entities;
namespace UserManager.Application.UnitTests.Features.Users
{
[TestClass]
public class UserServiceUnitTests
{
[TestMethod]
public async Task CreateUserAsync_ShouldCallAddAndSave_WhenCreateUserCalled()
{
// Arrange
Mock<IUnitOfWork> unitOfWork = new();
Mock<IUserRepository> userRepository = new();
unitOfWork.SetupGet(u => u.UserRepository).Returns(userRepository.Object);
var request = new CreateUserRequestDto()
{
FirstName = "John",
LastName = "Doe",
};
UserService userService = new(unitOfWork.Object);
// Act
CreateUserResponseDto responseDto = await userService.CreateUserAsync(request);
// Assert
unitOfWork.Verify(u => u.UserRepository.CreateAsync(It.IsAny<User>()), Times.Once);
unitOfWork.Verify(u => u.SaveChangesAsync(), Times.Once);
}
}
}