public async Task EditCatItem_Returns_UpdatedCartItem() { //Arrange var moq = new DbContextMock <MyAppDbContext>(dummyOptions); moq.CreateDbSetMock(x => x.BooksAsCartItems, new[] { new BookAsCartItem { Id = 1, Price = 10, Quantity = 2 }, new BookAsCartItem { Id = 2, Price = 20, Quantity = 1 } }); BookAsCartItemDTO cartItemToBeUpdated = new BookAsCartItemDTO() { Id = 1, Quantity = 5 //we updated the Quantity of CartItem with Id 1 (we changed the Quantity from 2 to 5) }; //Act BookAsCartItemService service = new BookAsCartItemService(moq.Object); BookAsCartItem cartItemWithId_1 = moq.Object.BooksAsCartItems.FirstOrDefault(i => i.Id == 1); await service.EditCartItem(cartItemToBeUpdated); //Assert Assert.Equal(5, cartItemWithId_1.Quantity); }
public async Task RemoveCartItem_DeletesCartItem_FromDatabase() { //Arrange var moq = new DbContextMock <MyAppDbContext>(dummyOptions); moq.CreateDbSetMock(x => x.BooksAsCartItems, new[] { new BookAsCartItem { Id = 1, Price = 10, Quantity = 2 }, new BookAsCartItem { Id = 2, Price = 20, Quantity = 1 } }); //Act BookAsCartItemService service = new BookAsCartItemService(moq.Object); BookAsCartItem cartItemToBeDeleted = moq.Object.BooksAsCartItems.FirstOrDefault(i => i.Id == 2); await service.RemoveCartItem(2); //Assert Assert.Equal(1, moq.Object.BooksAsCartItems.Count()); }
public void GetCart_WhenCalled_ReturnsAllCartItems_AndGrandTotalWithDiscount_IfTotalAmountIsAboveThreshold() { //Arrange var moq = new DbContextMock <MyAppDbContext>(dummyOptions); BookAsCartItem myCartItemOne = new BookAsCartItem() { Id = 1, Price = 50, Quantity = 2 }; BookAsCartItem myCartItemTwo = new BookAsCartItem() { Id = 2, Price = 60, Quantity = 1 }; List <BookAsCartItem> All_Books_As_Cart_Items_With_Discount = new List <BookAsCartItem>(); All_Books_As_Cart_Items_With_Discount.Add(myCartItemOne); All_Books_As_Cart_Items_With_Discount.Add(myCartItemTwo); moq.CreateDbSetMock(x => x.Carts, new[] { //When Total amount is more than 100 dollors then we have 10% discount new ShoppingCart { Id = 1, AllBooksInsideCart = All_Books_As_Cart_Items_With_Discount, Total = 160, Discount = 16, GrandTotal = 144 } }); //Act ShoppingCartService service = new ShoppingCartService(moq.Object); var my_Cart = service.GetCart(); //Assert Assert.NotNull(my_Cart); }
public async Task CreateCartItem(BookAsCartItemDTO favoriteBook) { BookAsCartItem chosenBook = new BookAsCartItem() { Id = favoriteBook.Id, Book = favoriteBook.Book, Price = favoriteBook.Price, Quantity = favoriteBook.Quantity, }; _context.BooksAsCartItems.Add(chosenBook); await _context.SaveChangesAsync(); await _context.BooksAsCartItems.Include(i => i.Book).Include(c => c.Book.Category).ToListAsync(); favoriteBook.Id = chosenBook.Id; favoriteBook.Book = chosenBook.Book;//Book will not be null in postman }