File size: 2,021 Bytes
de8b914
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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>();
    }
}