Example #1
0
        public async Task CreateCartItem_AddsNewProductToCartItemsTable()
        {
            //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
                }
            });

            //We want to add third product to our list of CartItems
            //Since CreateCartItem() method accepts type CartItemDTO we use that type here for our new CartItem
            CartItemDTO testDataDTO = new CartItemDTO()
            {
                ProductId = 3,
                Price     = 21,
                Quantity  = 1
            };

            CartItemService service = new CartItemService(myDbContextMoq.Object);

            //Act
            await service.CreateCartItem(testDataDTO);//call CreateCartItem() function and pass the testDataDTO

            //Assert
            //The size of the CartItems list increases to 3 because CreateCartItem() method added testDataDTO
            Assert.Equal(3, myDbContextMoq.Object.CartItems.Count());
        }