Esempio n. 1
0
        public void Cannot_Checkout_Empty_Cart()
        {
            // Arrange - Create a mock order processor
            Mock<IOrderProcessor> mock = new Mock<IOrderProcessor>();

            // Arrange - Create an empty cart
            Cart cart = new Cart();

            // Arrange - Create shipping details
            ShippingDetails shippingDetails = new ShippingDetails();

            // Arrange - Create an instance of the controller
            CartController target = new CartController(null, mock.Object);

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

            // Assert - Check that the order hasn't been passed onto 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 we are passing an invalid model to the view
            Assert.AreEqual(false, result.ViewData.ModelState.IsValid);
        }
Esempio n. 2
0
 public void Adding_Product_To_Cart_Goes_To_Cart_Screen()
 {
     // Arrange - create the mock repository
     Mock<IProductRepository> mock = new Mock<IProductRepository>();
     mock.Setup(m => m.Products).Returns(new Product[] {
         new Product {ProductID = 1, Name = "P1", Category = "Apples"},
     }.AsQueryable());
     // Arrange - create a Cart
     Cart cart = new Cart();
     // Arrange - create the controller
     CartController target = new CartController(mock.Object, null);
     // Act - add a product to the cart
     RedirectToRouteResult result = target.AddToCart(cart, 2, "myUrl");
     // Assert
     Assert.AreEqual(result.RouteValues["action"], "Index");
     Assert.AreEqual(result.RouteValues["returnUrl"], "myUrl");
 }
Esempio n. 3
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 Game(), 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 we are passing an invalid model to the view
            Assert.AreEqual(false, result.ViewData.ModelState.IsValid);
        }
Esempio n. 4
0
        public void Can_View_Cart_Contents()
        {
            // Arrange - Create a cart
            Cart cart = new Cart();

            // Arrange - Create the controller
            CartController target = new CartController(null, null);

            //Act - Call the Index action method
            CartIndexViewModel result = (CartIndexViewModel)target.Index(cart, "myUrl").ViewData.Model;

            // Assert
            Assert.AreSame(result.Cart, cart);
            Assert.AreEqual(result.ReturnUrl, "myUrl");
        }
Esempio n. 5
0
        public void Can_Checkout_And_Submit_Order()
        {
            // 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 Game(), 1);

            // Arrange - Create an instance of the controller
            CartController target = new CartController(null, mock.Object);

            // Act - Try to checkout
            ViewResult result = target.Checkout(cart, new ShippingDetails());

            // Assert - Check that the order has been passed onto the processor
            mock.Verify(m => m.ProcessOrder(It.IsAny<Cart>(), It.IsAny<ShippingDetails>()),
                Times.Once());

            // Assert - Check that the method is returning the Completed view
            Assert.AreEqual("Completed", result.ViewName);

            // Assert - Check that we are passing a valid model to the view
            Assert.AreEqual(true, result.ViewData.ModelState.IsValid);
        }
Esempio n. 6
0
        public void Can_Add_To_Cart()
        {
            // Arrange - Create the mock repository
            Mock<IGameRepository> mock = new Mock<IGameRepository>();
            mock.Setup(m => m.Games).Returns(new Game[]
            {
                new Game {GameID = 1, Name = "P1", Category = "Apples"},
            }.AsQueryable());

            // Arrange - Create a new cart
            Cart cart = new Cart();

            // Arrange - Create the controller
            CartController target = new CartController(mock.Object, null);

            // Act - Add a game to the cart
            target.AddToCart(cart, 1, null);

            // Assert
            Assert.AreEqual(cart.Lines.Count(), 1);
            Assert.AreEqual(cart.Lines.ToArray()[0].Game.GameID, 1);
        }
Esempio n. 7
0
 public void Can_Add_To_Cart()
 {
     // Arrange - create the mock repository
     Mock<IProductRepository> mock = new Mock<IProductRepository>();
     mock.Setup(m => m.Products).Returns(new Product[] {
         new Product {ProductID = 1, Name = "P1", Category = "Apples"},
     }.AsQueryable());
     // Arrange - create a Cart
     Cart cart = new Cart();
     // Arrange - create the controller
     CartController target = new CartController(mock.Object, null);
     // Act - add a product to the cart
     target.AddToCart(cart, 1, null);
     // Assert
     Assert.AreEqual(cart.Lines.Count(), 1);
     Assert.AreEqual(cart.Lines.ToArray()[0].Product.ProductID, 1);
 }