Example #1
0
        public void CanAddBasketItem()
        {
            IRepository <Basket>  baskets  = new MockContext <Basket>();
            IRepository <Product> products = new MockContext <Product>();
            IRepository <Order>   orders   = new MockContext <Order>();
            var httpContext = new MockHttpConext();

            IBasketService basketService = new BasketService(products, baskets);

            IOrderService orderService = new OrderService(orders);

            var controller = new BasketController(basketService, orderService);

            controller.ControllerContext = new System.Web.Mvc.ControllerContext(httpContext, new System.Web.Routing.RouteData(), controller);

            controller.AddToBasket("1");

            basketService.AddToBasket(httpContext, "1");

            Basket basket = baskets.Collection().FirstOrDefault();

            Assert.IsNotNull(basket);
            Assert.AreEqual(1, basket.BasketItems.Count());
            Assert.AreEqual("1", basket.BasketItems.ToList().FirstOrDefault().ProductId);
        }
Example #2
0
        public void CanGetSummaryViewModel()
        {
            IRepository <Basket>  baskets  = new MockContext <Basket>();
            IRepository <Product> products = new MockContext <Product>();
            IRepository <Order>   orders   = new MockContext <Order>();

            products.Insert(new Product()
            {
                Id = "1", Price = 10.00m
            });
            products.Insert(new Product()
            {
                Id = "2", Price = 20.00m
            });
            products.Insert(new Product()
            {
                Id = "3", Price = 30.00m
            });

            Basket basket = new Basket();

            basket.BasketItems.Add(new BasketItem()
            {
                ProductId = "1", Quantity = 2
            });
            basket.BasketItems.Add(new BasketItem()
            {
                ProductId = "2", Quantity = 3
            });
            basket.BasketItems.Add(new BasketItem()
            {
                ProductId = "3", Quantity = 4
            });

            baskets.Insert(basket);

            IBasketService basketService = new BasketService(products, baskets);

            IOrderService orderService = new OrderService(orders);

            var controller  = new BasketController(basketService, orderService);
            var httpContext = new MockHttpConext();

            httpContext.Request.Cookies.Add(new System.Web.HttpCookie("eCommerceBasket")
            {
                Value = basket.Id
            });

            controller.ControllerContext = new System.Web.Mvc.ControllerContext(httpContext, new System.Web.Routing.RouteData(), controller);

            var result        = controller.BasketSummary() as PartialViewResult;
            var basketSummary = (BasketSummaryViewModel)result.ViewData.Model;

            Assert.AreEqual(9, basketSummary.BasketCount);
            Assert.AreEqual(200, basketSummary.BasketTotal);
        }
Example #3
0
        public void CanCheckoutAndCreateOrder()
        {
            IRepository <Basket>  baskets  = new MockContext <Basket>();
            IRepository <Product> products = new MockContext <Product>();
            IRepository <Order>   orders   = new MockContext <Order>();

            products.Insert(new Product()
            {
                Id = "1", Price = 10.00m
            });
            products.Insert(new Product()
            {
                Id = "2", Price = 10.00m
            });

            Basket basket = new Basket();

            basket.BasketItems.Add(new BasketItem()
            {
                ProductId = "1", Quantity = 1
            });
            basket.BasketItems.Add(new BasketItem()
            {
                ProductId = "2", Quantity = 1
            });

            baskets.Insert(basket);

            var httpContext = new MockHttpConext();

            httpContext.Request.Cookies.Add(new System.Web.HttpCookie("eCommerceBasket")
            {
                Value = basket.Id
            });

            IBasketService basketService = new BasketService(products, baskets);
            IOrderService  orderService  = new OrderService(orders);

            var controller = new BasketController(basketService, orderService);

            controller.ControllerContext = new System.Web.Mvc.ControllerContext(httpContext, new System.Web.Routing.RouteData(), controller);

            Order order = new Order();

            controller.Checkout(order);

            Assert.AreEqual(2, order.OrderItems.Count);
            Assert.AreEqual(0, basket.BasketItems.Count);

            Order orderInRep = orders.Find(order.Id);

            Assert.AreEqual(2, orderInRep.OrderItems.Count);
        }