BudgetCategoryServiceTests.cs
2,593 bytes
| 1 | using App.BLL.DTO; |
|---|---|
| 2 | using App.BLL.Services; |
| 3 | using App.Domain; |
| 4 | using App.Domain.Contracts; |
| 5 | using Base.Domain; |
| 6 | using FluentAssertions; |
| 7 | using Moq; |
| 8 | |
| 9 | namespace App.Tests.BLL; |
| 10 | |
| 11 | public class BudgetCategoryServiceTests |
| 12 | { |
| 13 | private readonly Mock<IAppUnitOfWork> _uow = new(); |
| 14 | private readonly Mock<IBudgetCategoryRepository> _categories = new(); |
| 15 | private readonly Mock<ITripParticipantRepository> _participants = new(); |
| 16 | private readonly BudgetCategoryService _sut; |
| 17 | |
| 18 | public BudgetCategoryServiceTests() |
| 19 | { |
| 20 | _uow.Setup(u => u.BudgetCategories).Returns(_categories.Object); |
| 21 | _uow.Setup(u => u.TripParticipants).Returns(_participants.Object); |
| 22 | _sut = new BudgetCategoryService(_uow.Object); |
| 23 | } |
| 24 | |
| 25 | [Fact] |
| 26 | public async Task CreateAsync_WhenUserIsOrganizer_AddsCategoryAndReturnsIt() |
| 27 | { |
| 28 | // Arrange |
| 29 | var userId = Guid.NewGuid(); |
| 30 | var tripId = Guid.NewGuid(); |
| 31 | var input = new BudgetCategoryBllDto |
| 32 | { |
| 33 | TripId = tripId, |
| 34 | Name = new LangStr("Food"), |
| 35 | PlannedAmount = 500m, |
| 36 | DisplayOrder = 1 |
| 37 | }; |
| 38 | |
| 39 | _participants.Setup(r => r.IsOrganizerAsync(tripId, userId)).ReturnsAsync(true); |
| 40 | BudgetCategory? added = null; |
| 41 | _categories.Setup(r => r.Add(It.IsAny<BudgetCategory>())) |
| 42 | .Callback<BudgetCategory>(c => added = c) |
| 43 | .Returns((BudgetCategory c) => c); |
| 44 | _categories.Setup(r => r.GetByIdAsync(It.IsAny<Guid>())) |
| 45 | .ReturnsAsync(() => added); |
| 46 | |
| 47 | // Act |
| 48 | var (result, error) = await _sut.CreateAsync(input, userId); |
| 49 | |
| 50 | // Assert |
| 51 | error.Should().BeNull(); |
| 52 | result.Should().NotBeNull(); |
| 53 | result!.PlannedAmount.Should().Be(500m); |
| 54 | _categories.Verify(r => r.Add(It.IsAny<BudgetCategory>()), Times.Once); |
| 55 | _uow.Verify(u => u.SaveChangesAsync(), Times.Once); |
| 56 | } |
| 57 | |
| 58 | [Fact] |
| 59 | public async Task CreateAsync_WhenUserIsNotOrganizer_ReturnsForbiddenAndDoesNotAdd() |
| 60 | { |
| 61 | // Arrange |
| 62 | var userId = Guid.NewGuid(); |
| 63 | var input = new BudgetCategoryBllDto { TripId = Guid.NewGuid(), Name = new LangStr("Food") }; |
| 64 | _participants.Setup(r => r.IsOrganizerAsync(input.TripId, userId)).ReturnsAsync(false); |
| 65 | |
| 66 | // Act |
| 67 | var (result, error) = await _sut.CreateAsync(input, userId); |
| 68 | |
| 69 | // Assert |
| 70 | result.Should().BeNull(); |
| 71 | error.Should().Be("forbidden"); |
| 72 | _categories.Verify(r => r.Add(It.IsAny<BudgetCategory>()), Times.Never); |
| 73 | _uow.Verify(u => u.SaveChangesAsync(), Times.Never); |
| 74 | } |
| 75 | } |
| 76 | |