Ejemplo n.º 1
0
        public async Task TestIfProductQuantityIncreasesThrowsErrorUserNull()
        {
            var context        = PCHUBDbContextInMemoryInitializer.InitializeContext();
            var productService = new ProductServices(context);
            var shopService    = new ShopServices(context, productService);

            var productsInsideUserCart = new List <Product>();

            productsInsideUserCart.Add(new Product
            {
                Id        = "Product",
                Price     = 300,
                Title     = "Test Product",
                IsDeleted = false,
                CreatedOn = DateTime.Now,
            });


            productsInsideUserCart.Add(new Product
            {
                Id        = "Product1",
                Price     = 300,
                Title     = "Test Product1",
                IsDeleted = false,
                CreatedOn = DateTime.Now,
            });

            productsInsideUserCart.Add(new Product
            {
                Id        = "Product2",
                Price     = 300,
                Title     = "Test Product2",
                IsDeleted = false,
                CreatedOn = DateTime.Now,
            });

            await context.Products.AddRangeAsync(productsInsideUserCart);

            await context.SaveChangesAsync();

            await Assert.ThrowsAsync <NullReferenceException>(async() =>
            {
                await shopService.IncreaseProductQuantityAsync(null, "Product1", 3);
            });
        }
Ejemplo n.º 2
0
        public async Task TestIfProductQuantityIncreases(string username, int quantity)
        {
            //Arrange
            var context        = PCHUBDbContextInMemoryInitializer.InitializeContext();
            var productService = new ProductServices(context);
            var shopService    = new ShopServices(context, productService);

            var productsInsideUserCart = new List <Product>();

            productsInsideUserCart.Add(new Product
            {
                Id        = "Product",
                Price     = 300,
                Title     = "Test Product",
                IsDeleted = false,
                CreatedOn = DateTime.Now,
            });


            productsInsideUserCart.Add(new Product
            {
                Id        = "Product1",
                Price     = 300,
                Title     = "Test Product1",
                IsDeleted = false,
                CreatedOn = DateTime.Now,
            });

            productsInsideUserCart.Add(new Product
            {
                Id        = "Product2",
                Price     = 300,
                Title     = "Test Product2",
                IsDeleted = false,
                CreatedOn = DateTime.Now,
            });

            await context.Products.AddRangeAsync(productsInsideUserCart);

            await context.Users.AddAsync(new User
            {
                UserName      = username,
                Email         = "*****@*****.**",
                LastLoginDate = DateTime.UtcNow,
            });

            await context.SaveChangesAsync();

            await shopService.BuyProductAsync(username, "Product2");

            await shopService.BuyProductAsync(username, "Product");

            await shopService.BuyProductAsync(username, "Product1");

            await shopService.IncreaseProductQuantityAsync(username, "Product1", quantity);

            var user = await context.Users.FirstOrDefaultAsync(x => x.UserName == username);

            var productCart = user.Cart.ProductCarts.FirstOrDefault(x => x.Product.Id == "Product1");

            Assert.Equal(productCart.Quantity, quantity);
        }