public void Can_Checkout_And_Submit_Order() {

            // Arrange
            Mock<IOrderProcessor> mock = new Mock<IOrderProcessor>();

            // Arrange
            Cart cart = new Cart();
            
            cart.AddItem(new Product(), 1);
            
            // Arrange
            CartController target = new CartController(null, mock.Object);

            // Act
            ViewResult result = target.Checkout(cart, new ShippingDetails());
            
            // Assert
            mock.Verify(m => m.ProcessOrder(It.IsAny<Cart>(),
                It.IsAny<ShippingDetails>()),
                Times.Once());

            // Assert
            Assert.AreEqual("Completed", result.ViewName);

            // Assert
            Assert.AreEqual(true, result.ViewData.ModelState.IsValid);
        }
        public void Cannot_Checkout_Invalid_ShippingDetails() {

            // Arrange
            Mock<IOrderProcessor> mock = new Mock<IOrderProcessor>();
            // Arrange
            Cart cart = new Cart();
            cart.AddItem(new Product(), 1);

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

            // Arrange
            target.ModelState.AddModelError("error", "error");

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

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

            // Assert
            Assert.AreEqual("", result.ViewName);

            // Assert
            Assert.AreEqual(false, result.ViewData.ModelState.IsValid);
        }
        public void Cannot_Checkout_Empty_Cart() {
            
            // Arrange
            Mock<IOrderProcessor> mock = new Mock<IOrderProcessor>();

            // Arrange
            Cart cart = new Cart();

            // Arrange
            ShippingDetails shippingDetails = new ShippingDetails();

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

            // Act
            ViewResult result = target.Checkout(cart, shippingDetails);
            
            // Assert
            mock.Verify(m => m.ProcessOrder(It.IsAny<Cart>(),It.IsAny<ShippingDetails>()), Times.Never());
            
            // Assert
            Assert.AreEqual("", result.ViewName);
            
            // Assert
            Assert.AreEqual(false, result.ViewData.ModelState.IsValid);
        }
 public void Can_View_Cart_Contents() {
     
     // Arrange
     Cart cart = new Cart();
     
     // Arrange
     CartController target = new CartController(null);
     
     // Act
     CartIndexViewModel result = (CartIndexViewModel)target.Index(cart, "myUrl").ViewData.Model;
     
     // Assert
     Assert.AreSame(result.Cart, cart);
     Assert.AreEqual(result.ReturnUrl, "myUrl");
 }   
 public void Adding_Product_To_Cart_Goes_To_Cart_Screen() {
     // Arrange
     Mock<IProductRepository> mock = new Mock<IProductRepository>();
     mock.Setup(m => m.Products).Returns(new Product[] { new Product {ProductID = 1, Name = "P1", Category = "Apples"}, }.AsQueryable());
     
     // Arrange
     Cart cart = new Cart();
     
     // Arrange
     CartController target = new CartController(mock.Object);
     
     // Act
     RedirectToRouteResult result = target.AddToCart(cart, 2, "myUrl");
     
     // Assert
     Assert.AreEqual(result.RouteValues["action"], "Index");
     Assert.AreEqual(result.RouteValues["returnUrl"], "myUrl");
 }
        public void Can_Add_To_Cart() {
            
            // Arrange
            Mock<IProductRepository> mock = new Mock<IProductRepository>();

            mock.Setup(m => m.Products).Returns(new Product[] { new Product {ProductID = 1, Name = "P1", Category = "Apples"}, }.AsQueryable());

            // Arrange
            Cart cart = new Cart();

            // Arrange
            CartController target = new CartController(mock.Object);

            // Act
            target.AddToCart(cart, 1, null);

            // Assert
            Assert.AreEqual(cart.Lines.Count(), 1);

            Assert.AreEqual(cart.Lines.ToArray()[0].Product.ProductID, 1);

        }