public async Task CanAddToCart(SliceFixture fixture) { // Arrange var registeredUser = await RegisterUser(fixture); // Create him a product to add var createdProduct = await CreateSampleProduct(fixture); // Act var product = await fixture .ExecuteDbContextAsync(db => db .Products .FirstOrDefaultAsync(p => p.Name == createdProduct.Name)); var addToCartCommand = new AddToCart.Command { CurrentUserName = registeredUser.UserName, ProductId = product.Id }; await fixture.SendAsync(addToCartCommand); // Assert var user = await fixture .ExecuteDbContextAsync(db => db .Users .Include(u => u.Cart) .ThenInclude(c => c.OrderedItems) .ThenInclude(i => i.Product) .FirstOrDefaultAsync(u => u.UserName == registeredUser.UserName)); user.Cart.OrderedItems.Count.ShouldBe(1); user.Cart.OrderedItems.First().Product.Name.ShouldBe(product.Name); user.Cart.OrderedItems.First().Quantity.ShouldBe(1); }
private static async Task AddProductToCart(SliceFixture fixture, Register.Command registerUserCommand, Product productInDb) { var addToCartQuery = new AddToCart.Command() { CurrentUserName = registerUserCommand.UserName, ProductId = productInDb.Id }; await fixture.SendAsync(addToCartQuery); }
public async Task ClearsUserCartOnCompletion(SliceFixture fixture) { // Arrange var registerUserCommand = new Register.Command { FirstName = "John", UserName = "******", Password = "******" }; await fixture.SendAsync(registerUserCommand); var product = new Product { Name = "Product", Description = "Description", DateAdded = DateTime.Now, Category = new Category() { Name = "Category" }, Price = 100.00m }; await fixture.InsertAsync(product); var addItemQuery = new AddToCart.Command { CurrentUserName = registerUserCommand.UserName, ProductId = product.Id }; await fixture.SendAsync(addItemQuery); // Act var command = new Completed.Command { CurrentUserName = registerUserCommand.UserName }; await fixture.SendAsync(command); // Assert var userInDb = await fixture .ExecuteDbContextAsync(db => db .Users .Include(u => u.Cart) .ThenInclude(c => c.OrderedItems) .FirstOrDefaultAsync(u => u.UserName == registerUserCommand.UserName)); userInDb.Cart.OrderedItems.Count.ShouldBe(0); }
public async Task IndexIsCorrect(SliceFixture fixture) { // Arrange // Create a user whose cart we are going to request var registerUserCommand = new Register.Command { FirstName = "John", UserName = "******", Password = "******" }; await fixture.SendAsync(registerUserCommand); // Add him some items var category = new Category { Name = "Category" }; await fixture.InsertAsync(category); var createCommands = new List <Create.Command> { new Create.Command { Name = "Product1", Description = "Description", Price = 12.00m, Category = category, }, new Create.Command { Name = "Product2", Description = "Description", Price = 12.00m, Category = category, }, new Create.Command { Name = "Product3", Description = "Description", Price = 12.00m, Category = category, }, }; foreach (var command in createCommands) { await fixture.SendAsync(command); } var productsInDb = await fixture .ExecuteDbContextAsync(db => db .Products .OrderBy(p => p.DateAdded) .ToListAsync()); foreach (var product in productsInDb) { var addToCartCommand = new AddToCart.Command { CurrentUserName = registerUserCommand.UserName, ProductId = product.Id }; await fixture.SendAsync(addToCartCommand); } // Act var indexQuery = new Index.Query { CurrentUserName = registerUserCommand.UserName, ReturnUrl = "/" }; var result = await fixture.SendAsync(indexQuery); // Assert // Sort them in the same way the products in db are sorted so I can assert by index result.OrderedItems .OrderBy(i => i.Product.DateAdded); result.OrderedItems.Count.ShouldBe(productsInDb.Count); result.OrderedItems[0].Product.Id.ShouldBe(productsInDb[0].Id); result.OrderedItems[1].Product.Id.ShouldBe(productsInDb[1].Id); result.OrderedItems[2].Product.Id.ShouldBe(productsInDb[2].Id); }