public async Task AddAsync_CartItemDto_AddOne() { string dbName = "AddAsync_CartItemDto_AddOne"; // Arrange Product product1 = new Product { Id = 1, ProductName = "Racing Car", CategoryId = 1, Description = "Nice Racing Car", ImagePath = "abc.png", UnitPrice = 23.1 }; var cartId = Guid.NewGuid().ToString(); var context = new WingtipContext(new DbContextOptionsBuilder <WingtipContext>().UseInMemoryDatabase("WingtipToysDB").Options); context.Products.Add(product1); context.SaveChanges(); ICartService service = new CartService(context, _mapper); CartItemDto cartDto = new CartItemDto { CartId = cartId, ProductId = product1.Id, Quantity = 3 }; // Act string result = await service.AddAsync(cartDto); // Assert Assert.AreEqual(cartId, result); }
public async Task AddAsync_AddsItemToDatabase() { var numberOfItemsInDatabase = await _context.Carts.CountAsync(); await _service.AddAsync(new CartDto { Products = new List <ProductDto>(), TotalPrice = 1 }); _context.Carts.CountAsync().Result.Should().Be(numberOfItemsInDatabase + 1); _validator.VerifyAll(); }
public async Task AddAsync_AddOne_UpdateIfExists() { // Arrange Product product1 = new Product { Id = 1, ProductName = "Racing Car", CategoryId = 1, Description = "Nice Racing Car", ImagePath = "abc.png", UnitPrice = 23.1 }; var cartId = Guid.NewGuid().ToString(); CartItem cartItem = new CartItem { Id = Guid.NewGuid().ToString(), CartId = cartId, ProductId = product1.Id, Product = product1, Created = DateTime.Now, Quantity = 1 }; var context = new WingtipContext(new DbContextOptionsBuilder <WingtipContext>().UseInMemoryDatabase("WingtipToysDB").Options); context.Products.Add(product1); context.CartItems.Add(cartItem); context.SaveChanges(); ICartService service = new CartService(context, _mapper); CartItemDto cartDto = new CartItemDto { CartId = cartId, ProductId = product1.Id, Quantity = 3 }; // Act await service.AddAsync(cartDto); // Assert var tempList = context.CartItems.ToList(); Assert.AreEqual(1, tempList.Count); Assert.AreEqual(cartItem.Id, tempList[0].Id); Assert.AreEqual(cartItem.CartId, tempList[0].CartId); Assert.AreEqual(cartItem.ProductId, tempList[0].ProductId); Assert.AreEqual(3, tempList[0].Quantity); // Clean data context.Products.RemoveRange(context.Products); context.CartItems.RemoveRange(context.CartItems); context.SaveChanges(); context.Dispose(); }