using Contracts.DTO; using Contracts.Repositories; using Contracts.SearchModels; using Contracts.Services; using Contracts.ViewModels; using Moq; using Services.Support.Exceptions; namespace Services.Tests.Domain; public class SpendingGroupServiceTests { [Fact] public async Task Create_ReturnsSpendingGroupViewModel() { var spendingGroupRepoMock = new Mock(); spendingGroupRepoMock.Setup(repo => repo.Create(It.IsAny())).ReturnsAsync(new SpendingGroupDto()); var spendingGroupService = new SpendingGroupService(spendingGroupRepoMock.Object); var result = await spendingGroupService.Create(new()); spendingGroupRepoMock.Verify(repo => repo.Create(It.IsAny()), Times.Once); Assert.NotNull(result); Assert.IsType(result); } [Fact] public async Task GetList_ReturnsSpendingGroupViewModels() { var spendingGroupRepoMock = new Mock(); spendingGroupRepoMock.Setup(repo => repo.GetList(It.IsAny())).ReturnsAsync(_getAllSpendingGroups()); var spendingGroupService = new SpendingGroupService(spendingGroupRepoMock.Object); var result = await spendingGroupService.GetList(); spendingGroupRepoMock.Verify(repo => repo.GetList(It.IsAny()), Times.Once); Assert.NotNull(result); Assert.IsType>(result.ToList()); } [Fact] public async Task Delete_ReturnsSpendingGroupViewModel() { var spendingGroupRepoMock = new Mock(); spendingGroupRepoMock.Setup(repo => repo.Delete(It.IsAny())).ReturnsAsync(new SpendingGroupDto()); var spendingGroupService = new SpendingGroupService(spendingGroupRepoMock.Object); var result = await spendingGroupService.Delete(new()); spendingGroupRepoMock.Verify(repo => repo.Delete(It.IsAny()), Times.Once); Assert.NotNull(result); Assert.IsType(result); } [Fact] public void Delete_WhenSpendingGroupNotFound_ThrowsEntityNotFoundException() { var spendingGroupRepoMock = new Mock(); spendingGroupRepoMock.Setup(repo => repo.Delete(It.IsAny())).ReturnsAsync((SpendingGroupDto)null); var spendingGroupService = new SpendingGroupService(spendingGroupRepoMock.Object); Assert.ThrowsAsync(() => spendingGroupService.Delete(new())); } [Fact] public async Task Update_ReturnsSpendingGroupViewModel() { var spendingGroupRepoMock = new Mock(); spendingGroupRepoMock.Setup(repo => repo.Update(It.IsAny())).ReturnsAsync(new SpendingGroupDto()); var spendingGroupService = new SpendingGroupService(spendingGroupRepoMock.Object); var result = await spendingGroupService.Update(new()); spendingGroupRepoMock.Verify(repo => repo.Update(It.IsAny()), Times.Once); Assert.NotNull(result); Assert.IsType(result); } [Fact] public void Update_WhenSpendingGroupNotFound_ThrowsEntityNotFoundException() { var spendingGroupRepoMock = new Mock(); spendingGroupRepoMock.Setup(repo => repo.Update(It.IsAny())).ReturnsAsync((SpendingGroupDto)null); var spendingGroupService = new SpendingGroupService(spendingGroupRepoMock.Object); Assert.ThrowsAsync(() => spendingGroupService.Update(new())); } [Fact] public async Task GetDetails_ReturnsSpendingGroupViewModel() { var spendingGroupRepoMock = new Mock(); spendingGroupRepoMock.Setup(repo => repo.Get(It.IsAny())).ReturnsAsync(new SpendingGroupDto()); var spendingGroupService = new SpendingGroupService(spendingGroupRepoMock.Object); var result = await spendingGroupService.GetDetails(new()); spendingGroupRepoMock.Verify(repo => repo.Get(It.IsAny()), Times.Once); Assert.NotNull(result); Assert.IsType(result); } [Fact] public void GetDetails_WhenSpendingGroupNotFound_ThrowsEntityNotFoundException() { var spendingGroupRepoMock = new Mock(); spendingGroupRepoMock.Setup(repo => repo.Get(It.IsAny())).ReturnsAsync((SpendingGroupDto)null); var spendingGroupService = new SpendingGroupService(spendingGroupRepoMock.Object); Assert.ThrowsAsync(() => spendingGroupService.GetDetails(new())); } private IEnumerable _getAllSpendingGroups() => new List() { new() { Id = Guid.NewGuid(), Name = "Group 1" }, new() { Id = Guid.NewGuid(), Name = "Group 2" } }; }