public async Task CreateCartItem_AddsANewCartItem_ToBooksAsCartItemsTable() { //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 new_book = new BookAsCartItemDTO() { Id = 3, Price = 15, Quantity = 4 }; //Act BookAsCartItemService service = new BookAsCartItemService(moq.Object); await service.CreateCartItem(new_book); //Assert Assert.Equal(3, moq.Object.BooksAsCartItems.Count()); }
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 EditCartItem(BookAsCartItemDTO favoriteBook) { var favoriteBookToBeUpdated = _context.BooksAsCartItems.Include(b => b.Book).Include(x => x.Book.Category).Single(i => i.Id == favoriteBook.Id); favoriteBookToBeUpdated.Quantity = favoriteBook.Quantity;//update Qauntity favoriteBook.Book = favoriteBookToBeUpdated.Book; await _context.SaveChangesAsync(); }
public async Task <ActionResult> UpdateCartItem(BookAsCartItemDTO favoriteBook) { if (favoriteBook == null) { return(NotFound()); } await _service.EditCartItem(favoriteBook); return(Ok(favoriteBook)); }
public async Task PostACartItem_WhenValidObjectIsPassed_ReturnsOkResult() { //Arrange var moq = new Mock <IBookAsCartItemService>(); Category category = new Category() { CategoryId = 1, CategoryName = "Novel" }; //create a new book Book book = new Book() { Id = 3, Title = "Jerry's Travel Story", Author = "Maria x", CategoryId = 1, Category = category, ISBN = 1486910263, Price = 34, DateOfPublication = "2016-10-27" }; BookAsCartItemDTO cartItem = new BookAsCartItemDTO() { Id = 2, Book = book, Price = 34, Quantity = 1 }; moq.Setup(i => i.CreateCartItem(cartItem)); //Act var controller = new BookAsCartItemController(moq.Object); var response = await controller.PostACartItem(cartItem); //Assert Assert.IsType <OkObjectResult>(response); }
public async Task UpdateCartItem_WhenValidCartItemIsPassed_ReturnsOkResult() { //Arrange var moq = new Mock <IBookAsCartItemService>(); Category category = new Category() { CategoryId = 1, CategoryName = "Novel" }; //create a new book Book book = new Book() { Id = 1, Title = "Jerry's Success Story", Author = "James x", CategoryId = 1, Category = category, ISBN = 1234567890, Price = 15, DateOfPublication = "2018-06-03" }; BookAsCartItemDTO cartItem = new BookAsCartItemDTO() { Id = 1, Book = book, Price = 15, Quantity = 4 }; moq.Setup(r => r.EditCartItem(cartItem)); //Act var controller = new BookAsCartItemController(moq.Object); var ok_Result = await controller.UpdateCartItem(cartItem); //Assert Assert.IsType <OkObjectResult>(ok_Result); }
public async Task <ActionResult> PostACartItem(BookAsCartItemDTO favoriteBook) { if (favoriteBook == null) { return(NotFound()); } if (!ModelState.IsValid) { return(BadRequest()); } await _service.CreateCartItem(favoriteBook); return(Ok(favoriteBook)); }
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 }
//create dummy data private List <BookAsCartItemDTO> DummyCartItem() { List <BookAsCartItemDTO> my_List = new List <BookAsCartItemDTO>();//create a new list //Create a new category Category category = new Category() { CategoryId = 1, CategoryName = "Novel" }; //create a new book Book book1 = new Book() { Id = 1, Title = "Jerry's Success Story", Author = "James x", CategoryId = 1, Category = category, ISBN = 1234567890, Price = 15, DateOfPublication = "2018-06-03" }; //create a new book Book book2 = new Book() { Id = 2, Title = "Story of Two Good Friends", Author = "Mary Y", CategoryId = 1, Category = category, ISBN = 1020304050, Price = 25, DateOfPublication = "2019-03-15" }; BookAsCartItemDTO cartItem1 = new BookAsCartItemDTO() { Id = 1, Book = book1, Price = 15, Quantity = 4 }; BookAsCartItemDTO cartItem2 = new BookAsCartItemDTO() { Id = 2, Book = book2, Price = 25, Quantity = 3 }; my_List.Add(cartItem1); //add cart item to the list my_List.Add(cartItem2); //add cart item to the list return(my_List); }