public void CanGetSummaryViewModel() { ////Setup IRepository <Cart> carts = new MockContext <Cart>(); IRepository <Product> products = new MockContext <Product>(); IRepository <Order> orders = new MockContext <Order>(); // creating MockContext for Order IRepository <Customer> customers = new MockContext <Customer>(); //Creating MockContect repository for Customer (linking customers to orders process) //Consantiration on calculations //Manually adding some items into products database //Because what I am testing is not how to add up products just what gets returned when we call the Get summary method. products.Insert(new Product() { Id = "1", Price = 10.00m }); products.Insert(new Product() { Id = "2", Price = 5.00m }); //Creating cart and add items to it. //Because I need to make sure that the item to it running to the cart have a corresponding Id and price //so that our underlying GetSummeryViewModel method has all the information it needs. Cart cart = new Cart(); cart.CartItems.Add(new CartItem() { ProductId = "1", Quanity = 2 }); cart.CartItems.Add(new CartItem() { ProductId = "2", Quanity = 1 }); carts.Insert(cart); ICartService cartService = new CartService(products, carts); IOrderService orderService = new OrderService(orders); // Creating order service var controller = new CartController(cartService, orderService, customers); //Creating controller //orderService added for test ////Adding customers (linking customers to orders process) var httpContext = new MockHttpContext(); httpContext.Request.Cookies.Add(new System.Web.HttpCookie("eCommerceCart") { Value = cart.Id }); //Manually add cookies controller.ControllerContext = new System.Web.Mvc.ControllerContext(httpContext, new System.Web.Routing.RouteData(), controller); //Inject httpContext(cookies). var result = controller.CartSummary() as PartialViewResult; //Because I exposed the controller as a partial view. var cartSummary = (CartSummaryViewModel)result.ViewData.Model; //Assert //Testing Assert.AreEqual(3, cartSummary.CartCount); Assert.AreEqual(25.00m, cartSummary.CartTotal); }
public void Summary() { //arrange CartController cartController = new CartController(Mocks.GetProductRepository()); //act IActionResult result = cartController.CartSummary(Mocks.GetCart(), ""); //assert Assert.AreEqual(typeof(ViewResult), result.GetType()); ViewResult viewResult = (ViewResult)result; Assert.IsAssignableFrom <CartSummaryViewModel>(viewResult.ViewData.Model); }
public void CanGetCartSummaryViewModel() { IRepository <Cart> carts = new MockContext <Cart>(); 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 }); Cart cart = new Cart(); cart.CartItems.Add(new CartItem() { ProductId = "1", Quantity = 2 }); cart.CartItems.Add(new CartItem() { ProductId = "2", Quantity = 1 }); carts.Insert(cart); ICartService cartService = new CartService(products, carts); IOrderService orderService = new OrderService(Orders); var controller = new CartController(cartService, orderService, customers); var httpContext = new MockHttpContext(); httpContext.Request.Cookies.Add(new System.Web.HttpCookie("eCommercecart") { Value = cart.Id }); controller.ControllerContext = new System.Web.Mvc.ControllerContext(httpContext, new System.Web.Routing.RouteData(), controller); var result = controller.CartSummary() as PartialViewResult; var cartSummary = (CartSummaryViewModel)result.ViewData.Model; Assert.AreEqual(3, cartSummary.CartCount); Assert.AreEqual(25.00m, cartSummary.CartTotal); }