Пример #1
0
        public void CanCheckoutAndCreateOrder()
        {
            IRepository <Customer> customers = new MockContext <Customer>();
            IRepository <Product>  products  = new MockContext <Product>();

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

            IRepository <Basket> baskets = new MockContext <Basket>();
            Basket basket = new Basket();

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

            baskets.Insert(basket);

            IBasketService basketService = new BasketService(products, baskets);

            IRepository <Order> orders       = new MockContext <Order>();
            IOrderService       orderService = new OrderService(orders);

            customers.Insert(new Customer()
            {
                Id = "1", Email = "*****@*****.**", ZipCode = "90210"
            });

            IPrincipal FakeUser = new GenericPrincipal(new GenericIdentity("*****@*****.**", "Forms"), null);


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

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

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

            //Act
            Order order = new Order();

            controller.Checkout(order);

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

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

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