Ejemplo n.º 1
0
        public void CanGetSummaryViewModel()
        {
            IRepository<Basket> baskets = new MockContext<Basket>();
            IRepository<Product> products = new MockContext<Product>();
            IRepository<Order> orders = new MockContext<Order>();
            IRepository<Customer> customers = new MockContext<Customer>();

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

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

            IBasketService basketService = new BasketService(products, baskets);
            IOrderService orderService = new OrderService(orders);
            var controller = new BasketController(basketService, orderService, customers);

            var httpContext = new MockHttpContext();
            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(3, basketSummary.BasketCount);
            Assert.AreEqual(25.00m, basketSummary.BasketTotal);
        }
Ejemplo n.º 2
0
        public void CanGetSummaryViewModel()
        {
            IRepository <Basket>   baskets      = new MockContext <Basket>();
            IRepository <Product>  products     = new MockContext <Product>();
            IRepository <Order>    orderContext = new MockContext <Order>();
            IRepository <Customer> customers    = new MockContext <Customer>();

            var httpContext = new MockHttpContext();

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

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

            var basket = new Basket();

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



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

            customers.Insert(new Customer()
            {
                Id = "1", Email = "*****@*****.**"
            });
            IPrincipal FakeUser = new GenericPrincipal(new GenericIdentity("*****@*****.**", "forms"), null);

            httpContext.User = FakeUser;

            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(3, basketSummary.BasketCount);
            Assert.AreEqual(32, basketSummary.BasketTotal);
        }
Ejemplo n.º 3
0
        public void CanGetSummaryViewModel()
        {
            var baskets     = new MockContext <Basket>();
            var products    = new MockContext <Product>();
            var orders      = new MockContext <Order>();
            var httpContext = new MockHttpContext();
            var customers   = new MockContext <Customer>();

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

            var basket = new Basket();

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

            baskets.Insert(basket);

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


            customers.Insert(new Customer {
                Id = "1", Email = "*****@*****.**", ZipCode = "11000"
            });
            var fakeUser = new GenericPrincipal(new GenericIdentity("*****@*****.**", "Forms"), null);

            httpContext.User = fakeUser;
            var controller = new BasketController(basketService, orderService, customers);

            httpContext.Request.Cookies.Add(new HttpCookie("eCommerceBasket")
            {
                Value = basket.Id
            });
            controller.ControllerContext = new ControllerContext(httpContext, new RouteData(), controller);

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

            Assert.AreEqual(3, basketSummary.BasketCount);
            Assert.AreEqual(25.00m, basketSummary.BasketTotal);
        }
        public void CanGetSummaryParitalView()
        {
            //Setup
            IRepository <Product> products = new MockRepository <Product>();

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

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

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

            IBasketService         basketService = new BasketService(products, baskets);
            IRepository <Order>    orders        = new MockRepository <Order>();
            IRepository <Customer> customers     = new MockRepository <Customer>();

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

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

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

            //Act
            //if we were testing the service directly we would do this
            //var basketSummary = basketService.GetBasketSummary(httpContext);
            //to test the servce via the controller we do this
            var result        = controller.BasketSummary() as PartialViewResult;
            var basketSummary = (BasketSummaryViewModel)result.ViewData.Model;

            //Assert
            Assert.AreEqual(3, basketSummary.BasketCount);
            Assert.AreEqual(25.00m, basketSummary.BasketTotal);
        }
        public void CanGetSummaryViewModel()
        {
            IRepository <Basket>  baskets  = new MockContext <Basket>();
            IRepository <Product> products = new MockContext <Product>();

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

            //Create a basket
            Basket basket = new Basket();

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

            IBasketService basketService = new BasketService(products, baskets);

            //Create the controller
            var controller  = new BasketController(basketService);
            var httpContext = new MockHttpContext();


            //Inject in the cookies manually
            httpContext.Request.Cookies.Add(new System.Web.HttpCookie("eCommerceBasket")
            {
                Value = basket.Id
            });

            //Add http context the http controller
            controller.ControllerContext = new System.Web.Mvc.ControllerContext(httpContext, new System.Web.Routing.RouteData(), controller);

            //Call that method rto see what gets returned
            var result        = controller.BasketSummary() as PartialViewResult;
            var basketSummary = (BasketSummaryViewModel)result.ViewData.Model;

            //Assets
            Assert.AreEqual(3, basketSummary.BasketCount);
            Assert.AreEqual(25.00m, basketSummary.BasketTotal);
        }
        public void CanGetSummaryViewModel()
        {
            IRepository <Basket>  basketContext  = new MockContext <Basket>();
            IRepository <Product> productContext = new MockContext <Product>();

            productContext.Insert(new Product()
            {
                Id = "1", Price = 10
            });
            productContext.Insert(new Product()
            {
                Id = "2", Price = 5
            });
            productContext.Insert(new Product()
            {
                Id = "3", Price = 5
            });

            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 = 5
            });
            basketContext.Insert(basket);

            IBasketService basketService = new BasketService(productContext, basketContext);

            var controller  = new BasketController(basketService);
            var httpContext = new MockHttpContext();

            httpContext.Request.Cookies.Add(new System.Web.HttpCookie("eCommerce")
            {
                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(15, basketSummary.BasketCount);
            Assert.AreEqual(60, basketSummary.BasketTotal);
        }
Ejemplo n.º 7
0
        public void CanGetSummaryViewModel()
        {
            // setup
            IRepository <Basket>   baskets   = new MockContext <Basket>();
            IRepository <Product>  products  = new MockContext <Product>();
            IRepository <Order>    orders    = new MockContext <Order>();
            IRepository <Customer> customers = new MockContext <Customer>();

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

            // Act
            Basket basket = new Basket();

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

            IBasketService basketService = new BasketService(products, baskets);
            IOrderService  orderService  = new OrderService(orders);
            var            controller    = new BasketController(basketService, orderService, customers);// dependency injection
            //var controller = new BasketController(basketService); //this has been replaced by the line above which takes in orderService
            var httpContext = new MockHttpContext();

            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; // casts the result as PartialView
            var basketSummary = (BasketSummaryViewModel)result.ViewData.Model;

            // Assert
            Assert.AreEqual(3, basketSummary.BasketCount);
            Assert.AreEqual(25.00m, basketSummary.BasketTotal);
        }
Ejemplo n.º 8
0
        public void CanGetSummaryViewModel()
        {
            //Arrange
            IRepository <Product>  _productsContext = new Mocks.MockContext <Product>();
            IRepository <Basket>   _BasketContext   = new Mocks.MockContext <Basket>();
            IRepository <Order>    _OrderContext    = new Mocks.MockContext <Order>();
            IRepository <Customer> _CustomerContext = new Mocks.MockContext <Customer>();

            Basket         basket = new Basket();
            IBasketService basketService;
            IOrderService  orderService;
            var            httpContext = new Mocks.MockHttpContext();


            //Act
            _productsContext.Insert(new Product {
                Id = "1", Price = 10.00m
            });
            _productsContext.Insert(new Product {
                Id = "2", Price = 20.00m
            });
            basket.BasketItems.Add(new BasketItem {
                ProductId = "1", Quantity = 2
            });
            basket.BasketItems.Add(new BasketItem {
                ProductId = "2", Quantity = 1
            });
            _BasketContext.Insert(basket);
            basketService = new BasketService(_productsContext, _BasketContext);
            orderService  = new OrderService(_OrderContext);


            var controller = new BasketController(basketService, orderService, _CustomerContext);

            httpContext.Request.Cookies.Add(new HttpCookie("eCommerceBasket")
            {
                Value = basket.Id
            });
            controller.ControllerContext = new ControllerContext(httpContext, new System.Web.Routing.RouteData(), controller);
            var result        = controller.BasketSummary() as PartialViewResult;
            var basketSummary = (BasketSummaryViewModel)result.ViewData.Model;

            //Assert
            Assert.AreEqual(3, basketSummary.BasketCount);
            Assert.AreEqual(40, basketSummary.BasketTotal);
        }
Ejemplo n.º 9
0
        public void CanGetSummaryViewModel()
        {
            IRepository <Basket>  baskets  = new MockContext <Basket>();
            IRepository <Product> products = new MockContext <Product>();

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

            Basket basket = new Basket();

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

            IBasketService basketService = new BasketService(products, baskets);

            var controller  = new BasketController(basketService);
            var httpContext = new MockHttpContext();

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

            controller.ControllerContext = new ControllerContext(httpContext, new RouteData(), controller);

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

            Assert.AreEqual(14, basketSummary.BasketCount);
            Assert.AreEqual(180.00m, basketSummary.BasketTotal);
        }
Ejemplo n.º 10
0
        public void CanGetSummaryViewModel()
        {
            IRepository <Product>  ProductContext  = new Mocks.MockContext <Product>();
            IRepository <Basket>   BasketContext   = new Mocks.MockContext <Basket>();
            IRepository <Order>    orderContext    = new MockContext <Order>();
            IRepository <Customer> customerContext = new MockContext <Customer>();
            IBasketService         basketService   = new BasketService(BasketContext, ProductContext);
            HttpContextBase        httpContext     = new MockHttpContext();
            IOrderService          orderService    = new OrderService(orderContext);
            Basket           basket     = new Basket();
            BasketController controller = new BasketController(basketService, orderService, customerContext);

            ProductContext.Insert(new Product {
                Id = "1", Price = 3.00m
            });
            ProductContext.Insert(new Product {
                Id = "2", Price = 2.00m
            });
            basket.BasketItems.Add(new BasketItem {
                ProductId = "1", Quantity = 2
            });
            basket.BasketItems.Add(new BasketItem {
                ProductId = "2", Quantity = 2
            });
            BasketContext.Insert(basket);
            httpContext.Request.Cookies.Add(new System.Web.HttpCookie("eCommerceBasket2")
            {
                Value = basket.Id
            });
            controller.ControllerContext = new System.Web.Mvc.ControllerContext(httpContext, new System.Web.Routing.RouteData(), controller);

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

            Assert.IsNotNull(viewModel);
            Assert.AreEqual(4, viewModel.BasketCount);
            Assert.AreEqual(10.00m, viewModel.BasketTotal);
        }
Ejemplo n.º 11
0
        public void CanGetSummaryViewModel()
        {
            //Setup
            IRepository <Basket>   baskets  = new MockContext <Basket>();
            IRepository <Product>  products = new MockContext <Product>();
            IRepository <Order>    order    = new MockContext <Order>();
            IRepository <Customer> customer = new MockContext <Customer>();

            //Act
            products.Insert(new Product("1", "Cosa1", 5));
            products.Insert(new Product("2", "Cosa2", 10));
            products.Insert(new Product("3", "Cosa3", 15));

            Basket basket = new Basket();

            basket.BasketItems.Add(new BasketItem("1", "1", 2));
            basket.BasketItems.Add(new BasketItem("1", "2", 1));
            baskets.Insert(basket);
            IBasketService basketService = new BasketService(products, baskets);
            IOrderService  orderService  = new OrderService(order);

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

            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
            Assert.AreEqual(3, basketSummary.BasketCount);
            Assert.AreEqual(20, basketSummary.BasketTotal);
        }