public void Can_Checkout_And_Submit_Order() { // Arrange - create a mock order processor Mock <IOrderProcessor> mock = new Mock <IOrderProcessor>(); // Arrange - create a cart with an item Cart cart = new Cart(); cart.AddItem(new Product(), 1); // Arrange - create an instance of the controller CartController target = new CartController(null, mock.Object); // Act - try to checkout ViewResult result = target.CheckOut(cart, new ShippingDetails()); // Assert - check that the order has been passed on to the processor mock.Verify(m => m.ProcessOrder(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 I am passing a valid model to the view Assert.AreEqual(true, result.ViewData.ModelState.IsValid); }
public async Task CheckOut_ModelState_Invalid_View_with_Model() { var order_service_mock = new Mock <IOrderService>(); var cart_service_mock = new Mock <ICartServices>(); var controller = new CartController(cart_service_mock.Object); controller.ModelState.AddModelError("error", "InvalidModel"); const string expected_model_name = "Test order"; var order_view_model = new OrderViewModel() { Name = expected_model_name }; var result = await controller.CheckOut(order_view_model, order_service_mock.Object); var view_result = Assert.IsType <ViewResult>(result); var model = Assert.IsAssignableFrom <CartOrderViewModel>(view_result.Model); Assert.Equal(expected_model_name, model.Order.Name); }
public void Task_Get_CheckOut_Return_View() { Assert.Throws <NullReferenceException>(() => { //Arrange var controller = new CartController(context); int id = 1; var customers = new Customers() { CustomerId = 1, }; //Act var GetData = controller.CheckOut(id, customers); //Assert Assert.IsType <RedirectToActionResult>(GetData); }); }
public void Cannot_Checkout_Empty_Cart() { //Arrange - create a mock order processor Mock <IOrderProcessor> mock = new Mock <IOrderProcessor>(); //Arrange - create an empty cart Cart cart = new Cart(); //Arrange - create shipping details ShippingDetails shippingDetails = new ShippingDetails(); //Arrange - create an instance of the controller CartController target = new CartController(null, mock.Object); //Act ViewResult result = target.CheckOut(cart, shippingDetails); //Assert - check that the order hasn't been passed on to the processor mock.Verify(m => m.ProcessOrder(It.IsAny <Cart>(), It.IsAny <ShippingDetails>()), Times.Never()); //Assert - check that the method is returning the default view Assert.AreEqual("", result.ViewName); //Assert - check that I am passing an invalid model to the view Assert.AreEqual(false, result.ViewData.ModelState.IsValid); }
public void Chekout_ModelState_Invalid_Returns_ViewModel() { var cartServiceMock = new Mock <ICartService>(); var orderServiceMock = new Mock <IOrderService>(); var controller = new CartController(cartServiceMock.Object, orderServiceMock.Object); // Добавляем ошибку controller.ModelState.AddModelError("error", "InvalidModel"); const string expectedModelName = "Test order"; var result = controller.CheckOut(new OrderViewModel { Name = expectedModelName }); var viewResult = Assert.IsType <ViewResult>(result); var model = Assert.IsAssignableFrom <DetailsViewModel>(viewResult.ViewData.Model); Assert.Equal(expectedModelName, model.OrderViewModel.Name); }
public async Task CheckOut_ModelState_Invalid_Returns_ViewModel() { // Arrange var fakeCartService = new Mock <ICartService>(); var fakeOrderService = new Mock <IOrderService>(); var controller = new CartController(fakeCartService.Object); controller.ModelState.AddModelError("error", "InvalidModel"); const string EXPECTED_MODEL_NAME = "Test order"; // Act var result = await controller.CheckOut( new OrderViewModel { Name = EXPECTED_MODEL_NAME }, fakeOrderService.Object); // Assert var viewResult = Assert.IsType <ViewResult>(result); var model = Assert.IsAssignableFrom <CartOrderViewModel>(viewResult.Model); Assert.Equal(EXPECTED_MODEL_NAME, model.Order.Name); }
public void Cannot_Checkout_Invalid_Shipping_Details() { //Arrange - create test IOrderRepository Mock <IOrderProcessor> mock = new Mock <IOrderProcessor>(); //Arrange - create Cart Cart cart = new Cart(); cart.AddItem(new Product(), 1); //Arrange - create an instance of controller CartController controller = new CartController(null, mock.Object); //Arrange add an error to the controller controller.ModelState.AddModelError("error", "error"); //Act - 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.ProcessOrder(It.IsAny <Cart>(), It.IsAny <ShippingDetails>()), 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); }
public void Valid_Order_Goes_To_Submitter_And_Displays_Completed_View() { // Arrange var mockSubmitter = new Moq.Mock <IOrderSubmitter>(); CartController controller = new CartController(null, mockSubmitter.Object); Cart cart = new Cart(); cart.AddItem(new Product(), 1); var formData = new FormCollection { { "Name", "Steve" }, { "Line1", "123 My Street" }, { "Line2", "MyArea" }, { "Line3", "" }, { "City", "MyCity" }, { "State", "Some State" }, { "Zip", "123ABCDEF" }, { "Country", "Far far away" }, { "GiftWrap", bool.TrueString } }; // Act var result = controller.CheckOut(cart, formData); // Assert Assert.AreEqual("Completed", result.ViewName); mockSubmitter.Verify(x => x.SubmitOrder(cart)); Assert.AreEqual(0, cart.Lines.Count); }
public void CanCheckOutAndCreateOrder() { ////Setup IRepository <Product> products = new MockContext <Product>(); //Mock product repository created IRepository <Customer> customers = new MockContext <Customer>(); //Creating MockContect repository for Customer (linking customers to orders process) //Adding some basic information in repository //Added Id and Price because that's the only thing that will affect any of calculations products.Insert(new Product() { Id = "1", Price = 10.00m }); products.Insert(new Product() { Id = "2", Price = 8.00m }); products.Insert(new Product() { Id = "3", Price = 13.00m }); IRepository <Cart> carts = new MockContext <Cart>(); //Creating Mock Cart repository Cart cart = new Cart(); //New underlying Cart //Adding some basic information cart.CartItems.Add(new CartItem() { ProductId = "1", Quanity = 2, CartId = cart.Id }); cart.CartItems.Add(new CartItem() { ProductId = "1", Quanity = 1, CartId = cart.Id }); cart.CartItems.Add(new CartItem() { ProductId = "1", Quanity = 3, CartId = cart.Id }); //I need to add that cart to the cart repository carts.Insert(cart); //Creating CartService ICartService cartService = new CartService(products, carts); //Creating OrderService first I need again repository of order IRepository <Order> orders = new MockContext <Order>(); IOrderService orderService = new OrderService(orders); //if I wanted to test the order service directly. I could simply act on the order service itself. //but I am going to test controller again ////Adding FakeUser(linking customers to orders process) customers.Insert(new Customer() { Id = "1", Email = "*****@*****.**", ZipCode = "02129" }); ////Creating IPrincipal //built in class called GenericPrincipal takes in GenericIdentity that simply wants an email adress.(linking customers to orders process) //I Also need to tell it the type of authentication is Forms authentication(linking customers to orders process) //I used null here because when I create our generic principle it wants to know what roles it has(linking customers to orders process) //I am not using roles that can simply be null(linking customers to orders process) IPrincipal FakeUser = new GenericPrincipal(new GenericIdentity("*****@*****.**", "Forms"), null); //Adding additional information into create a CartController instance var controller = new CartController(cartService, orderService, customers); //controller expect cartService and OrderService //Adding customers (linking customers to orders process) var httpContext = new MockHttpContext(); //Injecting fake context so that it can read an write cookies //Creating cookie itself manually because i am creating cart manually httpContext.Request.Cookies.Add(new System.Web.HttpCookie("eCommerceCart") { Value = cart.Id }); httpContext.User = FakeUser;////Adding FakeUser(linking customers to orders process) //As last one I need to add httpContext to the underlying ControllerContext controller.ControllerContext = new ControllerContext(httpContext, new System.Web.Routing.RouteData(), controller); ////Act Order order = new Order();//creating order controller.CheckOut(order); ////Assert Assert.AreEqual(3, order.OrderItems.Count); //testing order items Assert.AreEqual(0, cart.CartItems.Count); //Make sure I have created an order and I want to clear the cart down Order orderInRep = orders.Find(order.Id); //try and retrieve the order from the repository. Assert.AreEqual(3, orderInRep.OrderItems.Count); //check 3 order items in the repository itself }