using Contracts.DTO; using Contracts.Repositories; using Contracts.SearchModels; using Contracts.ViewModels; using Moq; using Services.Domain; using Services.Support.Exceptions; namespace Services.Tests.Domain; public class SpendingPlanServiceTests { [Fact] public async Task Create_ReturnsSpendingPlanViewModel() { var spendingPlanRepoMock = new Mock(); spendingPlanRepoMock.Setup(repo => repo.Create(It.IsAny())).ReturnsAsync(new SpendingPlanDto()); var spendingPlanService = new SpendingPlanService(spendingPlanRepoMock.Object); var result = await spendingPlanService.Create(new()); spendingPlanRepoMock.Verify(repo => repo.Create(It.IsAny()), Times.Once); Assert.NotNull(result); Assert.IsType(result); } [Fact] public async Task Update_WhenPlanExists_ThenUpdatePlan_ReturnsSpendingPlanViewModel() { var spendingPlanRepoMock = new Mock(); spendingPlanRepoMock.Setup(repo => repo.Update(It.IsAny())) .ReturnsAsync(new SpendingPlanDto()); var spendingPlanService = new SpendingPlanService(spendingPlanRepoMock.Object); var result = await spendingPlanService.Update(new()); spendingPlanRepoMock.Verify(repo => repo.Update(It.IsAny()), Times.Once); Assert.NotNull(result); Assert.IsType(result); } [Fact] public async Task Update_WhenPlanNotFound_ThenThrowsEntityNotFoundException() { var spendingPlanRepoMock = new Mock(); spendingPlanRepoMock.Setup(repo => repo.Update(It.IsAny())) .ReturnsAsync((SpendingPlanDto)null); var spendingPlanService = new SpendingPlanService(spendingPlanRepoMock.Object); await Assert.ThrowsAsync(() => spendingPlanService.Update(new())); } [Fact] public async Task Delete_WhenPlanExists_ThenDeletePlan_ReturnsSpendingPlanViewModel() { var spendingPlanRepoMock = new Mock(); spendingPlanRepoMock.Setup(repo => repo.Delete(It.IsAny())) .ReturnsAsync(new SpendingPlanDto()); var spendingPlanService = new SpendingPlanService(spendingPlanRepoMock.Object); var result = await spendingPlanService.Delete(new()); spendingPlanRepoMock.Verify(repo => repo.Delete(It.IsAny()), Times.Once); Assert.NotNull(result); Assert.IsType(result); } [Fact] public async Task Delete_WhenPlanNotFound_ThenThrowsEntityNotFoundException() { var spendingPlanRepoMock = new Mock(); spendingPlanRepoMock.Setup(repo => repo.Delete(It.IsAny())) .ReturnsAsync((SpendingPlanDto)null); var spendingPlanService = new SpendingPlanService(spendingPlanRepoMock.Object); await Assert.ThrowsAsync(() => spendingPlanService.Delete(new())); } [Fact] public async Task GetList_ReturnsSpendingPlanViewModels() { var spendingPlanRepoMock = new Mock(); spendingPlanRepoMock.Setup(repo => repo.GetList(It.IsAny())) .ReturnsAsync(_getAllSpendingPlans()); var spendingPlanService = new SpendingPlanService(spendingPlanRepoMock.Object); var result = await spendingPlanService.GetList(new()); spendingPlanRepoMock.Verify(repo => repo.GetList(It.IsAny()), Times.Once); Assert.NotNull(result); Assert.IsType>(result.ToList()); Assert.Equal(2, result.Count()); } private IEnumerable _getAllSpendingPlans() => new List() { new() { Id = Guid.NewGuid(), StartAt = DateTime.Now, EndAt = DateTime.Now, Sum = 100 }, new() { Id = Guid.NewGuid(), StartAt = DateTime.Now, EndAt = DateTime.Now, Sum = 200 } }; }