using FluentAssertions; using NSubstitute; using SCM.Application.Common; using SCM.Application.Projects; using SCM.Application.Projects.Dtos; using SCM.Domain.Projects; using SCM.Shared.Exceptions; using Xunit; namespace SCM.UnitTests.Projects; public class ProjectServiceTests { private readonly IProjectRepository _repository = Substitute.For(); private readonly IUnitOfWork _unitOfWork = Substitute.For(); private readonly ProjectService _sut; public ProjectServiceTests() => _sut = new ProjectService(_repository, _unitOfWork); [Fact] public async Task CreateAsync_DeveLancarConflict_QuandoCodigoJaExiste() { _repository.CodeExistsAsync("OBRA-001", null, Arg.Any()) .Returns(true); var act = () => _sut.CreateAsync( new CreateProjectRequest("OBRA-001", "Obra", null, DateTime.Today, null)); await act.Should().ThrowAsync(); await _repository.DidNotReceive().AddAsync(Arg.Any(), Arg.Any()); } [Fact] public async Task CreateAsync_DevePersistirEmTransacao_QuandoValido() { _repository.CodeExistsAsync(Arg.Any(), null, Arg.Any()) .Returns(false); var response = await _sut.CreateAsync( new CreateProjectRequest("OBRA-001", "Obra", null, DateTime.Today, null)); response.Code.Should().Be("OBRA-001"); _unitOfWork.Received(1).BeginTransaction(); _unitOfWork.Received(1).Commit(); await _repository.Received(1).AddAsync(Arg.Any(), Arg.Any()); } [Fact] public async Task GetByIdAsync_DeveLancarNotFound_QuandoInexistente() { _repository.GetByIdAsync(Arg.Any(), Arg.Any()) .Returns((Project?)null); var act = () => _sut.GetByIdAsync(Guid.NewGuid()); await act.Should().ThrowAsync(); } }