public async Task HandleAsync_WithSomeItemsOfItemCategoryOnActiveShoppingLists_ShouldStoreItemsAndShoppingListsAndDeleteItemCategory() { // Arrange var fixture = commonFixture.GetNewFixture(); ItemCategoryRepositoryMock itemCategoryRepositoryMock = new ItemCategoryRepositoryMock(fixture); ItemRepositoryMock itemRepositoryMock = new ItemRepositoryMock(fixture); ShoppingListRepositoryMock shoppingListRepositoryMock = new ShoppingListRepositoryMock(fixture); TransactionGeneratorMock transactionGeneratorMock = new TransactionGeneratorMock(fixture); ItemCategoryMock itemCategoryMock = itemCategoryMockFixtur.Create(); var storeItemMocks = storeItemMockFixture.CreateMany(2).ToList(); var shoppingLists = new Dictionary <StoreItemMock, List <ShoppingListMock> >(); foreach (var storeItemMock in storeItemMocks) { int amount = commonFixture.NextInt(1, 5); var listMocks = shoppingListMockFixture.CreateMany(amount).ToList(); shoppingLists.Add(storeItemMock, listMocks); shoppingListRepositoryMock.SetupFindActiveByAsync(storeItemMock.Object.Id, listMocks.Select(m => m.Object)); } TransactionMock transactionMock = new TransactionMock(); var command = fixture.Create <DeleteItemCategoryCommand>(); var handler = fixture.Create <DeleteItemCategoryCommandHandler>(); itemCategoryRepositoryMock.SetupFindByAsync(command.ItemCategoryId, itemCategoryMock.Object); itemRepositoryMock.SetupFindActiveByAsync(command.ItemCategoryId, storeItemMocks.Select(m => m.Object)); transactionGeneratorMock.SetupGenerateAsync(transactionMock.Object); // Act var result = await handler.HandleAsync(command, default); // Assert using (new AssertionScope()) { result.Should().BeTrue(); itemCategoryMock.VerifyDeleteOnce(); foreach (var storeItemMock in storeItemMocks) { storeItemMock.VerifyDeleteOnce(); itemRepositoryMock.VerifyStoreAsyncOnce(storeItemMock.Object); IEnumerable <ShoppingListMock> affiliatedShoppingListMocks = shoppingLists[storeItemMock]; foreach (var listMock in affiliatedShoppingListMocks) { listMock.VerifyRemoveItemOnce(storeItemMock.Object.Id); shoppingListRepositoryMock.VerifyStoreAsyncOnce(listMock.Object); } } itemCategoryRepositoryMock.VerifyStoreAsyncOnce(itemCategoryMock.Object); transactionMock.VerifyCommitAsyncOnce(); } }
public async Task HandleAsync_WithSomeItemsOfItemCategoryOnNoActiveShoppingLists_ShouldStoreItemsButNoShoppingListsAndDeleteItemCategory() { // Arrange var fixture = commonFixture.GetNewFixture(); ItemCategoryRepositoryMock itemCategoryRepositoryMock = new ItemCategoryRepositoryMock(fixture); ItemRepositoryMock itemRepositoryMock = new ItemRepositoryMock(fixture); ShoppingListRepositoryMock shoppingListRepositoryMock = new ShoppingListRepositoryMock(fixture); TransactionGeneratorMock transactionGeneratorMock = new TransactionGeneratorMock(fixture); ItemCategoryMock itemCategoryMock = itemCategoryMockFixtur.Create(); List <StoreItemMock> storeItemMocks = storeItemMockFixture.CreateMany(3).ToList(); TransactionMock transactionMock = new TransactionMock(); var command = fixture.Create <DeleteItemCategoryCommand>(); var handler = fixture.Create <DeleteItemCategoryCommandHandler>(); itemCategoryRepositoryMock.SetupFindByAsync(command.ItemCategoryId, itemCategoryMock.Object); itemRepositoryMock.SetupFindActiveByAsync(command.ItemCategoryId, storeItemMocks.Select(m => m.Object)); shoppingListRepositoryMock.SetupFindActiveByAsync(Enumerable.Empty <IShoppingList>()); transactionGeneratorMock.SetupGenerateAsync(transactionMock.Object); // Act var result = await handler.HandleAsync(command, default); // Assert using (new AssertionScope()) { result.Should().BeTrue(); itemCategoryMock.VerifyDeleteOnce(); shoppingListRepositoryMock.VerifyStoreAsyncNever(); foreach (var storeItemMock in storeItemMocks) { storeItemMock.VerifyDeleteOnce(); itemRepositoryMock.VerifyStoreAsyncOnce(storeItemMock.Object); } itemCategoryRepositoryMock.VerifyStoreAsyncOnce(itemCategoryMock.Object); transactionMock.VerifyCommitAsyncOnce(); } }