Beispiel #1
0
        public async Task AddProductToCart_WithValidData_ShouldAddProductSuccessfully()
        {
            string onFalseErrorMessage              = "The method returned false upon valid data input for creation.";
            string onNullMethodReturnErrorMessage   = "The shopping cart was not created for the user before his first product.";
            string onNullDatabaseReturnErrorMessage = "The product was not added to the shopping cart.";

            var context = ApplicationDbContextInMemoryFactory.InitializeContext();

            var productService      = new Mock <IProductService>();
            var shoppingCartService = new ShoppingCartService(context, productService.Object);

            // Giving only valid data
            var productId = await this.SeedSingleProduct(context);

            var userId = await this.SeedSingleUser(context);

            var quantity = 2;

            var methodResult = await shoppingCartService.AddProductToCart(productId, userId, quantity);

            Assert.True(methodResult, onFalseErrorMessage);

            // Ensure that the shopping cart is created for the user before adding the first product
            var userShoppingCart = await context.ShoppingCarts.FirstOrDefaultAsync(sc => sc.ApplicationUserId == userId);

            AssertExtensions.NotNullWithMessage(
                userShoppingCart, onNullMethodReturnErrorMessage);

            // Ensure that the product has been added to the cart successfully
            var productInCart = userShoppingCart.ShoppingCartProducts
                                .FirstOrDefault(p => p.ProductId == productId && p.Quantity == quantity);

            AssertExtensions.NotNullWithMessage(
                productInCart, onNullDatabaseReturnErrorMessage);
        }
Beispiel #2
0
        public async Task AddProductToCart_WithInvalidProduct_ShouldReturnFalse()
        {
            string onTrueErrorMessage = "The method returned true with invalid product.";

            var context = ApplicationDbContextInMemoryFactory.InitializeContext();

            var productService      = new Mock <IProductService>();
            var shoppingCartService = new ShoppingCartService(context, productService.Object);

            // Not seeding the product
            var productId = "FakeProductId";
            var userId    = await this.SeedSingleUser(context);

            var quantity = 2;

            var methodResult = await shoppingCartService.AddProductToCart(productId, userId, quantity);

            Assert.False(methodResult, onTrueErrorMessage);
        }