Exemple #1
0
        public void CanAddToCart()
        {
            // Arrange - create the mock repository
            var mock = new Mock<IProductRepository>();
            mock.Setup(m => m.Products).Returns(new[]
                {
                    new Product {ProductId = 1, Name = "P1", CategoryId = 1},
                }.AsQueryable());
            // Arrange - create a Cart
            var cart = new Cart();
            // Arrange - create the controller
            var target = new CartController(mock.Object, null);

            // Action - 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);
        }
Exemple #2
0
        public void CannotAddNewProductFromDifferentEstablishment()
        {
            // Arrange - create the mock repository
            var mock = new Mock<IProductRepository>();
            mock.Setup(m => m.Products).Returns(new[]
                {
                    new Product {ProductId = 1, Name = "P1", EstablishmentId = 1},
                    new Product {ProductId = 2, Name = "P2", EstablishmentId = 2}
                }.AsQueryable());
            // Arrange - create a Cart
            var cart = new Cart();
            // Arrange - create the controller
            var target = new CartController(mock.Object, null);

            // Action - add a product to the cart
            target.AddToCart(cart, 1, null);
            try
            {
                target.AddToCart(cart, 2, null);
                Assert.Fail(); // If we get to this line we have failed.
            }
            catch (Exception)
            {
                // If we add a throw; here the test does not run.
            }


            // Assert
            Assert.AreEqual(cart.Lines.Count(), 1);
            Assert.AreEqual(cart.Lines.ToArray()[0].Product.ProductId, 1);
        }
Exemple #3
0
        public void AddingProductToCartGoesToCartScreen()
        {
            // Arrange - create the mock repository
            var mock = new Mock<IProductRepository>();
            mock.Setup(m => m.Products).Returns(new[]
                {
                    new Product {ProductId = 1, Name = "P1", CategoryId = 1},
                }.AsQueryable());
            // Arrange - create a Cart
            var cart = new Cart();
            // Arrange - create the controller
            var target = new CartController(mock.Object, null);

            // Action - 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");
        }
Exemple #4
0
        public void CanViewCartContents()
        {
            // Arrange - create a Cart
            var cart = new Cart();
            // Arrange - create the controller
            var target = new CartController(null, null);

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

            // Assert
            Assert.AreSame(result.Cart, cart);
            Assert.AreEqual(result.ReturnUrl, "myUrl");
        }
Exemple #5
0
        public void CanCheckoutAndSubmitOrder()
        {
            // Arrange - create a mock order processor
            var mockRepository = new Mock<IProductRepository>();
            var what = new Mock<ICartApplicationService>();
            // Arrange - create a cart with an item
            var cart = new Cart();
            var shippingDetails = new ShippingDetails
                {
                    Name = "Jason",
                    Line1 = "123 Fake",
                    City = "Corpus Christi",
                    State = "Texas",
                    Zip = "78414",
                    Country = "United States",
                    GiftWrap = false
                };
            cart.AddItem(new Product {ProductId = 1, Name = "P1", Price = 100M, EstablishmentId = 1, CategoryId = 1, Description = "none"}, 1);
            // Arrange - create an instance of the controller

            var controller = new CartController(mockRepository.Object, what.Object);

            // Action - try to checkout
            var result = controller.Checkout(cart, shippingDetails);

            // Assert - check that the order has been passed on to the processor
            what.Verify(m => m.Process(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);
        }
Exemple #6
0
        public void CannotCheckoutInvalidShippingDetails()
        {
            // Arrange - create a mock order processor
            var mock = new Mock<IOrderProcessor>();
            // Arrange - create a cart with an item
            var cart = new Cart();
            cart.AddItem(new Product(), 1);
            // Arrange - create an instance of the controller
            var controller = new CartController(null, null);
            // Arrange - add an error to the model
            controller.ModelState.AddModelError("error", "error");

            // Action - try to checkout
            ViewResult result = controller.Checkout(cart, new ShippingDetails());

            // Assert - check that the order hasn't been passed on to the processor
            mock.Verify(m => m.CreateOrder(It.IsAny<Order>()),
                        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);
        }
Exemple #7
0
        public void CannotCheckoutEmptyCart()
        {
            // Arrange - create a mock order processor
            var mock = new Mock<IOrderProcessor>();
            // Arrange - create an empty cart
            var cart = new Cart();
            // Arrange - create shipping details
            var shippingDetails = new ShippingDetails();

            // Arrange - create an instance of the controller
            var controller = new CartController(null, null);

            // Action
            ViewResult result = controller.Checkout(cart, shippingDetails);

            // Assert - check that the order hasn't been passed on to the processor
            mock.Verify(m => m.CreateOrder(It.IsAny<Order>()),
                        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);
        }