From 6838555dd28d83caab2244d1427e442cb07994de Mon Sep 17 00:00:00 2001 From: Jason Zhu Date: Wed, 22 Sep 2021 16:14:07 +1000 Subject: [PATCH] 9.3.5 Completing the Cart Controller; UNIT TEST: ORDER PROCESSING --- .../SportsStore.UnitTests/CartTests.cs | 88 ++++++++++++++++++- 1 file changed, 84 insertions(+), 4 deletions(-) diff --git a/SportsStore/SportsStore.UnitTests/CartTests.cs b/SportsStore/SportsStore.UnitTests/CartTests.cs index b2c0381..5540143 100644 --- a/SportsStore/SportsStore.UnitTests/CartTests.cs +++ b/SportsStore/SportsStore.UnitTests/CartTests.cs @@ -137,7 +137,7 @@ namespace SportsStore.UnitTests Cart cart = new Cart(); // Arrange - create the controller - CartController target = new CartController(mock.Object); + CartController target = new CartController(mock.Object, null); // Act - add a product to the cart target.AddToCart(cart, 1, null); @@ -161,7 +161,7 @@ namespace SportsStore.UnitTests Cart cart = new Cart(); // Arrange - create the controller - CartController target = new CartController(mock.Object); + CartController target = new CartController(mock.Object, null); // Act - add a product to the RedirectToRouteResult result = target.AddToCart(cart, 2, "myUrl"); @@ -178,7 +178,7 @@ namespace SportsStore.UnitTests Cart cart = new Cart(); // Arrange - create the controller - CartController target = new CartController(null); + CartController target = new CartController(null, null); // Act - call the Index action methdo CartIndexViewModel result = (CartIndexViewModel)target.Index(cart, "myUrl").ViewData.Model; @@ -187,5 +187,85 @@ namespace SportsStore.UnitTests Assert.AreSame(result.Cart, cart); Assert.AreEqual(result.ReturnUrl, "myUrl"); } -} + + [TestMethod] + public void Cannot_Checkout_Empty_Cart() + { + // Arrange - create a mock order processor + Mock mock = new Mock(); + // 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(), It.IsAny()), Times.Never()); + + // Assert - check that 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); + } + + [TestMethod] + public void Cannot_Checkout_Invalid_ShippingDetails() + { + + // Arrange - create a mock order processor + Mock mock = new Mock(); + + // 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); + // use null to inject repo, as we don't need it + + // Arrange - add an error to the model + target.ModelState.AddModelError("error", "error"); + + // Act - try to checkout + ViewResult result = target.Checkout(cart, new ShippingDetails()); + + // Assert - check that the order hasn't been passed on to the processor + mock.Verify(m => m.ProcessOrder(It.IsAny(), It.IsAny()), + 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); + } + + [TestMethod] + public void Can_Checkout_And_Submit_Order() + { + + // Arrange - create a mock order processor + Mock mock = new Mock(); + + // 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(), It.IsAny()), + 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); + } + } }