scm-api / tests /SCM.UnitTests /Projects /ProjectServiceTests.cs
rafaeldavila's picture
Deploy SCM API (.NET 8) - Docker Space
de8b914 verified
Raw History Blame Contribute Delete
2.02 kB
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<IProjectRepository>();
private readonly IUnitOfWork _unitOfWork = Substitute.For<IUnitOfWork>();
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<CancellationToken>())
.Returns(true);
var act = () => _sut.CreateAsync(
new CreateProjectRequest("OBRA-001", "Obra", null, DateTime.Today, null));
await act.Should().ThrowAsync<ConflictException>();
await _repository.DidNotReceive().AddAsync(Arg.Any<Project>(), Arg.Any<CancellationToken>());
}
[Fact]
public async Task CreateAsync_DevePersistirEmTransacao_QuandoValido()
{
_repository.CodeExistsAsync(Arg.Any<string>(), null, Arg.Any<CancellationToken>())
.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<Project>(), Arg.Any<CancellationToken>());
}
[Fact]
public async Task GetByIdAsync_DeveLancarNotFound_QuandoInexistente()
{
_repository.GetByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>())
.Returns((Project?)null);
var act = () => _sut.GetByIdAsync(Guid.NewGuid());
await act.Should().ThrowAsync<NotFoundException>();
}
}