public async Task ShouldFailToRemoveFoodTableIdNotFound() { RemoveFoodTableCommand command = new RemoveFoodTableCommand { FoodTableId = Guid.NewGuid() }; Mock <IFoodTableRepository> foodTableRepository = new Mock <IFoodTableRepository>(); foodTableRepository.Setup(repo => repo.GetCustomByIdAsync(It.IsAny <Guid>(), It.IsAny <Guid>())) .ReturnsAsync(() => null); FoodTableCommandHandler handler = new FoodTableCommandHandler( foodTableRepository.Object, GetIdentityService(), GetMediator(), GetUnitOfWork(), GetLogger() ); CommandResult commandResult = await handler.Handle(command, default(CancellationToken)); Assert.IsFalse(commandResult.Success); Assert.AreEqual("Nenhuma categoria de alimentos com esse Id foi encontrada no banco de dados.", commandResult.Notifications.FirstOrDefault().Description); }
public async Task ShouldRemoveFoodTable() { Guid profileId = Guid.NewGuid(); CustomFoodTable existingFoodTable = new CustomFoodTable(profileId, "Bacon inicial", "Bacon antes de sofrer os testes"); bool isRemoved = false; Mock <IFoodTableRepository> foodTableRepository = new Mock <IFoodTableRepository>(); foodTableRepository.Setup(repo => repo.GetCustomByIdAsync(It.IsAny <Guid>(), It.IsAny <Guid>())) .ReturnsAsync(() => { if (!isRemoved) { return(existingFoodTable); } return(null); }); foodTableRepository.Setup(repo => repo.RemoveAsync(It.IsAny <CustomFoodTable>())) .Callback((CustomFoodTable foodTable) => { isRemoved = true; }); RemoveFoodTableCommand command = new RemoveFoodTableCommand { FoodTableId = existingFoodTable.Id, Name = "Bacon após testes", Description = "Registrando um bacon de testes", }; CustomFoodTable insertedFoodTable = await foodTableRepository.Object.GetCustomByIdAsync(existingFoodTable.Id, profileId); Assert.AreEqual("Bacon inicial", insertedFoodTable.Name); FoodTableCommandHandler handler = new FoodTableCommandHandler( foodTableRepository.Object, GetIdentityService(profileId), GetMediator(), GetUnitOfWork(), GetLogger() ); CommandResult commandResult = await handler.Handle(command, default(CancellationToken)); CustomFoodTable updatedInsertedFoodTable = await foodTableRepository.Object.GetCustomByIdAsync(Guid.NewGuid(), profileId); Assert.IsTrue(commandResult.Success); Assert.IsNull(updatedInsertedFoodTable); }
public async Task ShouldRegisterFoodTable() { RegisterFoodTableCommand command = new RegisterFoodTableCommand { Name = "Bacon de testes", Description = "Registrando um bacon de testes", }; Mock <IFoodTableRepository> foodTableRepository = new Mock <IFoodTableRepository>(); FoodTableCommandHandler handler = new FoodTableCommandHandler( foodTableRepository.Object, GetIdentityService(null), GetMediator(), GetUnitOfWork(), GetLogger() ); CommandResult commandResult = await handler.Handle(command, default(CancellationToken)); Assert.IsTrue(commandResult.Success); }
public async Task ShouldFailToUpdateFoodTableUserNotOwner() { Guid profileId = Guid.NewGuid(); CustomFoodTable existingFoodTable = new CustomFoodTable(profileId, "Bacon inicial", "Bacon antes de sofrer os testes"); UpdateFoodTableCommand command = new UpdateFoodTableCommand { FoodTableId = existingFoodTable.Id, Name = "Bacon de testes", Description = "Registrando um bacon de testes", }; Mock <IFoodTableRepository> foodTableRepository = new Mock <IFoodTableRepository>(); foodTableRepository.Setup(repo => repo.GetCustomByIdAsync(It.IsAny <Guid>(), It.IsAny <Guid>())) .ReturnsAsync((Guid id, Guid pid) => { if (pid == existingFoodTable.ProfileId && id == existingFoodTable.Id) { return(existingFoodTable); } return(null); }); FoodTableCommandHandler handler = new FoodTableCommandHandler( foodTableRepository.Object, GetIdentityService(), GetMediator(), GetUnitOfWork(), GetLogger() ); CommandResult commandResult = await handler.Handle(command, default(CancellationToken)); Assert.IsFalse(commandResult.Success); Assert.AreEqual("Nenhuma categoria de alimentos com esse Id foi encontrada no banco de dados.", commandResult.Notifications.FirstOrDefault().Description); }