Exemplo n.º 1
0
 public void ClearTest_CanClearContents()
 {
     Cart target = new Cart();
     target.AddItem(p1, 1);
     target.AddItem(p2, 1);
     target.Clear();
     Assert.AreEqual(target.Lines.Count(), 0);
 }
Exemplo n.º 2
0
        public void ComputeTotalValueTest_CalculateCartTotal()
        {
            Cart target = new Cart();
            target.AddItem(p1, 1);
            target.AddItem(p2, 1);
            target.AddItem(p1, 3);
            decimal result = target.ComputeTotalValue();

            Assert.AreEqual(result, 450M);
        }
Exemplo n.º 3
0
        public void AddItemTest_CanAddNewLines()
        {
            var target = new Cart();
            target.AddItem(p1, 1);
            target.AddItem(p2, 1);
            CartLine[] results = target.Lines.ToArray();

            Assert.AreEqual(results.Length, 2);
            Assert.AreEqual(results[0].Product, p1);
            Assert.AreEqual(results[1].Product, p2);
        }
Exemplo n.º 4
0
        public void AddItemTest_CanAddQuantityForExistingLines()
        {
            var target = new Cart();
            target.AddItem(p1, 1);
            target.AddItem(p2, 1);
            target.AddItem(p1, 10);
            CartLine[] results = target.Lines.OrderBy(c => c.Product.ProductID).ToArray();

            Assert.AreEqual(results.Length, 2);
            Assert.AreEqual(results[0].Quantity, 11);
            Assert.AreEqual(results[1].Quantity, 1);
        }
Exemplo n.º 5
0
        public void RemoveLineTest_CanRemoveLine()
        {
            var target = new Cart();
            target.AddItem(p1, 1);
            target.AddItem(p2, 3);
            target.AddItem(p3, 5);
            target.AddItem(p2, 1);
            target.RemoveLine(p2);

            Assert.AreEqual(target.Lines.Where(c => c.Product == p2).Count(), 0);
            Assert.AreEqual(target.Lines.Count(), 2);
        }
Exemplo n.º 6
0
        public void CanClearContent()
        {
            Product product1 = new Product { ProductId = 1, Name = "Product1", Price = 100m };
            Product product2 = new Product { ProductId = 2, Name = "Product2", Price = 50m };

            Cart target = new Cart();
            target.AddItem(product1, 1);
            target.AddItem(product2, 1);

            target.Clear();

            Assert.AreEqual(target.Lines.Count(), 0);
        }
Exemplo n.º 7
0
        public void Calculate_Cart_Total()
        {
            Product p1 = new Product { ProductID = 1, Name = "P1", Price = 100M };
            Product p2 = new Product { ProductID = 2, Name = "P2", Price =50M};

            Cart target = new Cart();

            target.AddItem(p1, 2);
            target.AddItem(p2, 2);
            decimal result = target.ComputeTotalValue();

            Assert.AreEqual(result, 300M);
        }
Exemplo n.º 8
0
        public void CanAddNewLines()
        {
            Product product1 = new Product { ProductId = 1, Name = "Product1" };
            Product product2 = new Product { ProductId = 2, Name = "Product2" };

            Cart target = new Cart();
            target.AddItem(product1, 1);
            target.AddItem(product2, 1);
            CartLine[] result = target.Lines.ToArray();

            Assert.AreEqual(result.Length, 2);
            Assert.AreEqual(result[0].Product, product1);
            Assert.AreEqual(result[1].Product, product2);
        }
Exemplo n.º 9
0
        public void CalcuateCartTotal()
        {
            Product product1 = new Product { ProductId = 1, Name = "Product1", Price = 100m };
            Product product2 = new Product { ProductId = 2, Name = "Product2", Price = 50m };

            Cart target = new Cart();
            target.AddItem(product1, 1);
            target.AddItem(product2, 1);
            target.AddItem(product1, 3);

            decimal actual = target.ComputeTotalValue();
            decimal expected = 450m;

            Assert.AreEqual(actual, expected);
        }
Exemplo n.º 10
0
 public void Calculate_Cart_Total()
 {
     // Arrange - create some test products
     Product p1 = new Product { ProductID = 1, Name = "P1", Price = 100M };
     Product p2 = new Product { ProductID = 2, Name = "P2", Price = 50M };
     // Arrange - create a new cart
     Cart target = new Cart();
     // Act
     target.AddItem(p1, 1);
     target.AddItem(p2, 1);
     target.AddItem(p1, 3);
     decimal result = target.ComputeTotalValue();
     // Assert
     Assert.AreEqual(result, 450M);
 }
Exemplo n.º 11
0
       public void Can_Add_New_Lines()
        {
            Product p1 = new Product { ProductID = 1, Name = "P1" };
            Product p2 = new Product { ProductID = 2, Name = "P2" };

            Cart target = new Cart();

            target.AddItem(p1, 1);
            target.AddItem(p2, 1);
            CartLine[] results = target.Lines.ToArray();

            Assert.AreEqual(results.Length , 2);
            Assert.AreEqual(results[0].Product, p1);
            Assert.AreEqual(results[1].Product, p2);
        }
Exemplo n.º 12
0
        public void Can_Add_Quantity_For_Existing_Item()
        {
            Product p1 = new Product { ProductID = 1, Name = "P1" };
            Product p2 = new Product { ProductID = 2, Name = "P2" };

            Cart target = new Cart();

            target.AddItem(p1, 1);
            target.AddItem(p2, 1);
            target.AddItem(p1, 1);
            CartLine[] results = target.Lines.ToArray();

            Assert.AreEqual(results.Length, 2);
            Assert.AreEqual(results[0].Quantity, 2);
        }
Exemplo n.º 13
0
        public void CanAddQuantityForExistingLines()
        {
            Product product1 = new Product { ProductId = 1, Name = "Product1" };
            Product product2 = new Product { ProductId = 2, Name = "Product2" };

            Cart target = new Cart();
            target.AddItem(product1, 1);
            target.AddItem(product2, 1);
            target.AddItem(product1, 10);

            CartLine[] result = target.Lines.OrderBy(c => c.Product.ProductId).ToArray();

            Assert.AreEqual(result.Length, 2);
            Assert.AreEqual(result[0].Quantity, 11);
            Assert.AreEqual(result[1].Quantity, 1);
        }
Exemplo n.º 14
0
 public void Can_Add_New_Lines()
 {
     // Arrange - create some test products
     Product p1 = new Product { ProductID = 1, Name = "P1" };
     Product p2 = new Product { ProductID = 2, Name = "P2" };
     // Arrange - create a new cart
     Cart target = new Cart();
     // Act
     target.AddItem(p1, 1);
     target.AddItem(p2, 1);
     CartLine[] results = target.Lines.ToArray();
     // Assert
     Assert.AreEqual(results.Length, 2);
     Assert.AreEqual(results[0].Product, p1);
     Assert.AreEqual(results[1].Product, p2);
 }
Exemplo n.º 15
0
        public void Calculate_Cart_Total()
        {
            // arrange
            Game game1 = new Game { GameId = 1, Name = "Sport1" };
            Game game2 = new Game { GameId = 2, Name = "Sport2" };
            Game game3 = new Game { GameId = 3, Name = "Sport3" };

            Cart cart = new Cart();
            //Act
            cart.AddItem(game1, 1);
            cart.AddItem(game2, 1);
            cart.AddItem(game1, 5);
            cart.Clear();

            // Assert
            Assert.AreEqual(cart.Lines.Count(), 0);
        }
Exemplo n.º 16
0
 public void Can_Add_Quantity_For_Existing_Lines()
 {
     // Arrange - create some test products
     Product p1 = new Product { ProductID = 1, Name = "P1" };
     Product p2 = new Product { ProductID = 2, Name = "P2" };
     // Arrange - create a new cart
     Cart target = new Cart();
     // Act
     target.AddItem(p1, 4);
     target.AddItem(p2, 1);
     target.AddItem(p1, 3);
     CartLine[] results = target.Lines.OrderBy(c => c.Product.ProductID).ToArray();
     // Assert
     Assert.AreEqual(results.Length, 2);
     Assert.AreEqual(results[0].Quantity, 7);
     Assert.AreEqual(results[1].Quantity, 1);
 }
Exemplo n.º 17
0
        public void Can_Remove_Lines()
        {
            Product p1 = new Product { ProductID = 1, Name = "P1" };
            Product p2 = new Product { ProductID = 2, Name = "P2" };
            Product p3 = new Product { ProductID = 3, Name = "P3" };

            Cart target = new Cart();

            target.AddItem(p1, 3);
            target.AddItem(p2, 1);
            target.RemoveLine(p2);
            target.AddItem(p3, 3);

            Assert.AreEqual(target.Lines.Where(c => c.Product == p2).Count(), 0);
            Assert.AreEqual(target.Lines.Where(c => c.Product == p1).Count(), 1);

            Assert.AreEqual(target.Lines.Count(), 2);

        }
Exemplo n.º 18
0
 public RedirectToRouteResult AddToCart(Cart cart, int productId, string returnUrl)
 {
     Product product = repository.Products
         .FirstOrDefault(p => p.ProductID == productId);
         if(product!=null)
         {
            cart.AddItem(product, 1);
         }
     return RedirectToAction("Index", new {returnUrl});
 }
Exemplo n.º 19
0
        public PartialViewResult AddToCart(Cart cart, int productId, string returnUrl)
        {
            Product product = repository.Products
                .FirstOrDefault(p => p.ProductId == productId);
            if (product != null)
            {
                cart.AddItem(product, 1);
            }

            return PartialView("Summary", cart);
        }
Exemplo n.º 20
0
 public void Can_Clear_Contents()
 {
     // Arrange - create some test products
     Product p1 = new Product
     {
         ProductID = 1,
         Name = "P1",
         Price = 100M
     };
     Product p2 = new Product { ProductID = 2, Name = "P2", Price = 50M };
     // Arrange - create a new cart
     Cart target = new Cart();
     // Arrange - add some items
     target.AddItem(p1, 1);
     target.AddItem(p2, 1);
     // Act - reset the cart
     target.Clear();
     // Assert
     Assert.AreEqual(target.Lines.Count(), 0);
 }
Exemplo n.º 21
0
        public RedirectToRouteResult AddToCart(Cart cart, int gameId, string returnUrl)
        {
            Game game = repository.Games
                .FirstOrDefault(g => g.GameId == gameId);

            if (game != null)
            {
               cart.AddItem(game, 1);
            }
            return RedirectToAction("Index", new { returnUrl });
        }
Exemplo n.º 22
0
        public void Cannot_Checkout_Invalid_ShippingDetails()
        {
            //Arrange
            Mock<IOrderProcessor> mock = new Mock<IOrderProcessor>();
            Cart cart = new Cart();
            cart.AddItem(new Product(), 1);

            CartController target = new CartController(null, mock.Object);
            target.ModelState.AddModelError("error", "error");

            //Act
            ViewResult result = target.Checkout(cart, new ShippingDetails());

            mock.Verify(m => m.ProcessOrder(It.IsAny<Cart>(), It.IsAny<ShippingDetails>()), Times.Never);
            Assert.AreEqual("", result.ViewName);
            Assert.AreEqual(false, result.ViewData.ModelState.IsValid);
        }
        public void CheckoutTest_CannotCheckoutInvalidShippingDetails()
        {
            var mock = new Mock<IOrderProcessor>();
            var cart = new Cart();
            cart.AddItem(new Product(), 1);

            var target = new CartController(null, mock.Object);
            //准备--把一个错误添加到模型
            target.ModelState.AddModelError("error", "error");
            //动作--试图结算
            ViewResult result = target.Checkout(cart, new ShippingDetails());

            //断言--检查,订单尚未传递给处理器
            mock.Verify(m => m.ProcessOrder(It.IsAny<Cart>(), It.IsAny<ShippingDetails>()), Times.Never);
            //断言--检查,该方法返回的默认视图
            Assert.AreEqual("", result.ViewName);
            //断言--检查,对视图传递一个非法模型
            Assert.AreEqual(false, result.ViewData.ModelState.IsValid);
        }
Exemplo n.º 24
0
 public void Cannot_Checkout_Invalid_ShippingDetails()
 {
     // Arrange - create a mock order processor
     Mock<IOrderProcessor> mock = new Mock<IOrderProcessor>();
     // Arrange - create a cart with an item
     Cart cart = new Cart();
     cart.AddItem(new Product(), 1);
     // Arrange - create an instance of the controller
     CartController target = new CartController(null, mock.Object);
     // Arrange - add an error to the model
     target.ModelState.AddModelError("error", "error");
     // Act - try to checkout
     ViewResult result = target.Checkout(cart, new ShippingDetails());
     // Assert - check that the order hasn't been passed on to the processor
     mock.Verify(m => m.ProcessOrder(It.IsAny<Cart>(), It.IsAny<ShippingDetails>()), Times.Never());
     // Assert - check that the method is returning the default view
     Assert.AreEqual("", result.ViewName);
     // Assert - check that I am passing an invalid model to the view
     Assert.AreEqual(false, result.ViewData.ModelState.IsValid);
 }
Exemplo n.º 25
0
        public void Can_Checkout_And_Submit_Order()
        {
            Mock<IOrderProcessor> mock = new Mock<IOrderProcessor>();

            Cart cart = new Cart();
            cart.AddItem(new Product(), 1); 

            CartController target = new CartController(null, mock.Object);

            ViewResult result = target.Checkout(cart, new ShippingDetails());

            mock.Verify(m => m.ProcessOrder(It.IsAny<Cart>(), It.IsAny<ShippingDetails>()), Times.Once);

            Assert.AreEqual("Completed", result.ViewName);
            Assert.AreEqual(true, result.ViewData.ModelState.IsValid);

        }
Exemplo n.º 26
0
        public void Can_Add_Quantity_For_Existing_Lines()
        {
            // arrange
            Game game1 = new Game { GameId = 1, Name = "Sport1" };
            Game game2 = new Game { GameId = 2, Name = "Sport2" };

            Cart cart = new Cart();
            //Act
            cart.AddItem(game1, 1);
            cart.AddItem(game2, 1);
            cart.AddItem(game1, 5);
            List<CartLine> results = cart.Lines.OrderBy(c => c.Game.GameId).ToList();

            // Assetr
            Assert.AreEqual(results.Count(), 2);
            Assert.AreEqual(results[0].Quantity, 6);    // 6 экземпляров добавлено в корзину
            Assert.AreEqual(results[1].Quantity, 1);
        }
Exemplo n.º 27
0
        public void Can_Remove_Line()
        {
            // arrange
            Game game1 = new Game { GameId = 1, Name = "Sport1" };
            Game game2 = new Game { GameId = 2, Name = "Sport2" };
            Game game3 = new Game { GameId = 3, Name = "Sport3" };

            Cart cart = new Cart();
            //Act

            cart.AddItem(game1, 1);
            cart.AddItem(game2, 4);
            cart.AddItem(game3, 2);
            cart.AddItem(game2, 1);

            //Act
            cart.RemoveLine(game2);

            //Assert
            Assert.AreEqual(cart.Lines.Where(c => c.Game == game2).Count(), 0);
            Assert.AreEqual(cart.Lines.Count(), 2);
        }
Exemplo n.º 28
0
        public void Can_Add_New_Lines()
        {
            // arrange
            Game game1 = new Game { GameId = 1, Name = "Sport1" };
            Game game2 = new Game { GameId = 2, Name = "Sport2" };

            Cart cart = new Cart();

            // Act
            cart.AddItem(game1, 1);
            cart.AddItem(game2, 1);
            List<CartLine> results = cart.Lines.ToList();

            // Assert
            Assert.AreEqual(results.Count(), 2);
            Assert.AreEqual(results[0].Game, game1);
            Assert.AreEqual(results[1].Game, game2);
        }
Exemplo n.º 29
0
 public void Can_Remove_Line()
 {
     // Arrange - create some test products
     Product p1 = new Product { ProductID = 1, Name = "P1" };
     Product p2 = new Product { ProductID = 2, Name = "P2" };
     Product p3 = new Product { ProductID = 3, Name = "P3" };
     // Arrange - create a new cart
     Cart target = new Cart();
     // Arrange - add some products to the cart
     target.AddItem(p1, 1);
     target.AddItem(p2, 3);
     target.AddItem(p3, 5);
     target.AddItem(p2, 1);
     // Act
     target.RemoveLine(p2);
     // Assert
     Assert.AreEqual(target.Lines.Where(c => c.Product == p2).Count(), 0);
     Assert.AreEqual(target.Lines.Count(), 2);
 }
Exemplo n.º 30
0
        public void Can_Clear_Contents()
        {
            Product p1 = new Product { ProductID = 1, Name = "P1", Price = 100M };
            Product p2 = new Product { ProductID = 2, Name = "P2", Price = 50M };

            Cart target = new Cart();

            target.AddItem(p2, 5);
            target.AddItem(p1, 2);
            target.AddItem(p2, 1);
            target.Clear();

            Assert.AreEqual(target.Lines.Count(), 0);
        }