Esempio n. 1
0
        public async Task UpdateCartItem_EditsTheCartItem_AndAddsTheUpdatedCartItemToCartItemsTable()
        {
            //Arrange
            var myDbContextMoq = new DbContextMock <ShoppingCartContext>(myDummyOptions);

            //Create list of CartItems that contains only two Products
            myDbContextMoq.CreateDbSetMock(x => x.CartItems, new[]
            {
                new CartItem {
                    ProductId = 1, Price = 3, Quantity = 2
                },
                new CartItem {
                    ProductId = 2, Price = 2, Quantity = 5
                }
            });

            CartItemDTO testDataDTO = new CartItemDTO()
            {
                ProductId = 1,
                Price     = 3,
                Quantity  = 4 //we updated the Quantity
            };

            CartItemService service = new CartItemService(myDbContextMoq.Object);

            //Act
            //for example we want to update first CartItem
            await service.UpdateCartItem(testDataDTO);

            CartItem cartItmeToBeUpdated = myDbContextMoq.Object.CartItems.FirstOrDefault(x => x.ProductId == 1);

            //Assert
            //Quantity changed from 2 to 4
            Assert.Equal(4, cartItmeToBeUpdated.Quantity);
        }
        // PUT api/<controller>
        public CartItemViewModel Put(CartItemViewModel cartItem)
        {
            _cartItemService.UpdateCartItem(mapper.Map <CartItemViewModel, CartItem>(cartItem));

            return(cartItem);
        }