public async Task HandleAsync_WithValidData_ShouldStoreItem() { // Arrange var local = new LocalFixture(); var handler = local.CreateCommandHandler(); IStoreItem storeItem = local.StoreItemFixture.CreateValid(); IStoreItemAvailability availability = local.CommonFixture.ChooseRandom(storeItem.Availabilities); var command = local.CreateCommand(availability); local.StoreItemFactoryMock.SetupCreate(command.TemporaryItemCreation, storeItem); // Act var result = await handler.HandleAsync(command, default); // Assert using (new AssertionScope()) { result.Should().BeTrue(); local.AvailabilityValidationServiceMock.VerifyValidateOnce( command.TemporaryItemCreation.Availability.ToMonoList()); local.ItemRepositoryMock.VerifyStoreAsyncOnce(storeItem); } }
public async Task HandleAsync_WithValidDataAndManufacturerId_ShouldMakeTemporaryItemPermanent() { // Arrange var local = new LocalFixture(); var handler = local.CreateCommandHandler(); StoreItemMock storeItemMock = local.StoreItemMockFixture.Create(StoreItemDefinition.FromTemporary(true)); List <IStoreItemAvailability> availabilities = storeItemMock.Object.Availabilities.ToList(); var command = local.CreateCommand(availabilities); local.ItemRepositoryMock.SetupFindByAsync(command.PermanentItem.Id, storeItemMock.Object); // Act var result = await handler.HandleAsync(command, default); // Assert using (new AssertionScope()) { result.Should().BeTrue(); local.ItemCategoryValidationServiceMock.VerifyValidateAsyncOnce(command.PermanentItem.ItemCategoryId); local.ManufacturerValidationServiceMock.VerifyValidateAsyncOnce(command.PermanentItem.ManufacturerId); local.AvailabilityValidationServiceMock.VerifyValidateOnce(availabilities); storeItemMock.VerifyMakePermanentOnce(command.PermanentItem, availabilities); local.ItemRepositoryMock.VerifyStoreAsyncOnce(storeItemMock.Object); } }
public async Task HandleAsync_WithValidDataAndManufacturerId_ShouldCreateItem() { // Arrange var local = new LocalFixture(); IStoreItem storeItem = local.StoreItemFixture.CreateValid(); List <IStoreItemAvailability> availabilities = storeItem.Availabilities.ToList(); var handler = local.CreateCommandHandler(); var command = local.CreateCommand(availabilities); local.StoreItemFactoryMock.SetupCreate(command.ItemCreation, storeItem); // Act var result = await handler.HandleAsync(command, default); // Assert using (new AssertionScope()) { result.Should().BeTrue(); local.ItemRepositoryMock.VerifyStoreAsyncOnce(storeItem); local.ItemCategoryValidationServiceMock.VerifyValidateAsyncOnce(command.ItemCreation.ItemCategoryId); local.ManufacturerValidationServiceMock.VerifyValidateAsyncOnce(command.ItemCreation.ManufacturerId); local.AvailabilityValidationServiceMock.VerifyValidateOnce(availabilities); } }
public async Task HandleAsync_WithInvalidItemId_ShouldThrowDomainException() { // Arrange var local = new LocalFixture(); var handler = local.CreateCommandHandler(); var command = local.CreateCommand(); local.ItemRepositoryMock.SetupFindByAsync(command.PermanentItem.Id, null); // Act Func <Task <bool> > action = async() => await handler.HandleAsync(command, default); // Assert using (new AssertionScope()) { (await action.Should().ThrowAsync <DomainException>()) .Where(ex => ex.Reason.ErrorCode == ErrorReasonCode.ItemNotFound); } }
public async Task HandleAsync_WithItemOnShoppingLists_ShouldDeleteItemAndRemoveItFromActiveShoppingLists( List <ShoppingListMock> shoppingListMocks) { // Arrange var local = new LocalFixture(); var handler = local.CreateCommandHandler(); var command = local.CreateCommand(); TransactionMock transactionMock = new TransactionMock(); StoreItemMock storeItemMock = local.StoreItemMockFixture.Create(); local.ItemRepositoryMock.SetupFindByAsync(command.ItemId, storeItemMock.Object); local.ShoppingListRepositoryMock.SetupFindActiveByAsync(storeItemMock.Object.Id, shoppingListMocks.Select(m => m.Object)); local.TransactionGeneratorMock.SetupGenerateAsync(transactionMock.Object); // Act var result = await handler.HandleAsync(command, default); // Assert using (new AssertionScope()) { result.Should().BeTrue(); storeItemMock.VerifyDeleteOnce(); local.ItemRepositoryMock.VerifyStoreAsyncOnce(storeItemMock.Object); if (!shoppingListMocks.Any()) { local.ShoppingListRepositoryMock.VerifyStoreAsyncNever(); } else { foreach (var shoppingListMock in shoppingListMocks) { shoppingListMock.VerifyRemoveItemOnce(storeItemMock.Object.Id); local.ShoppingListRepositoryMock.VerifyStoreAsyncOnce(shoppingListMock.Object); } } transactionMock.VerifyCommitAsyncOnce(); } }
public async Task HandleAsync_WithNonTemporaryItem_ShouldThrowDomainException() { // Arrange var local = new LocalFixture(); var handler = local.CreateCommandHandler(); var command = local.CreateCommand(); var baseDefinition = StoreItemDefinition.FromTemporary(false); IStoreItem storeItem = local.StoreItemFixture.CreateValid(baseDefinition); local.ItemRepositoryMock.SetupFindByAsync(command.PermanentItem.Id, storeItem); // Act Func <Task <bool> > action = async() => await handler.HandleAsync(command, default); // Assert using (new AssertionScope()) { (await action.Should().ThrowAsync <DomainException>()) .Where(ex => ex.Reason.ErrorCode == ErrorReasonCode.ItemNotTemporary); } }