public void CanAddNewLines() { // Arrange - create some test Products var p1 = new Product {ProductId = 1, Name = "P1"}; var p2 = new Product {ProductId = 2, Name = "P2"}; // Arrange - create a new Cart var target = new Cart(); // Action target.AddItem(p1, 1); target.AddItem(p2, 1); CartLine[] results = target.Lines.ToArray(); // Assert Assert.AreEqual(results.Length, 2); Assert.AreEqual(results[0].Product, p1); Assert.AreEqual(results[1].Product, p2); }
public void CanAddQuantityForExistingLines() { // Arrange - create some test products var p1 = new Product {ProductId = 1, Name = "P1"}; var p2 = new Product {ProductId = 2, Name = "P2"}; // Arrange - create a new cart var target = new Cart(); // Action target.AddItem(p1, 1); target.AddItem(p2, 1); target.AddItem(p1, 10); CartLine[] results = target.Lines.OrderBy(c => c.Product.ProductId).ToArray(); // Assert Assert.AreEqual(results.Length, 2); Assert.AreEqual(results[0].Quantity, 11); Assert.AreEqual(results[1].Quantity, 1); }
public RedirectToRouteResult AddToCart(Cart cart, int productId, string returnUrl) { Product product = this._repository.Products.FirstOrDefault(p => p.ProductId == productId); if (product != null) { cart.AddItem(product, 1); } return RedirectToAction("Index", new { returnUrl }); }
public void CanRemoveLine() { // Arrange - create some test products var p1 = new Product {ProductId = 1, Name = "P1"}; var p2 = new Product {ProductId = 2, Name = "P2"}; var p3 = new Product {ProductId = 3, Name = "P3"}; // Arrange - create a new cart var target = new Cart(); // Arrange - add some products to the cart target.AddItem(p1, 1); target.AddItem(p2, 3); target.AddItem(p3, 5); target.AddItem(p2, 1); // Action target.RemoveLine(p2); // Assert Assert.AreEqual(target.Lines.Count(c => c.Product == p2), 0); Assert.AreEqual(target.Lines.Count(), 2); }
public void CanCheckoutAndSubmitOrder() { // Arrange - create a mock order processor var mockRepository = new Mock<IProductRepository>(); var what = new Mock<ICartApplicationService>(); // Arrange - create a cart with an item var cart = new Cart(); var shippingDetails = new ShippingDetails { Name = "Jason", Line1 = "123 Fake", City = "Corpus Christi", State = "Texas", Zip = "78414", Country = "United States", GiftWrap = false }; cart.AddItem(new Product {ProductId = 1, Name = "P1", Price = 100M, EstablishmentId = 1, CategoryId = 1, Description = "none"}, 1); // Arrange - create an instance of the controller var controller = new CartController(mockRepository.Object, what.Object); // Action - try to checkout var result = controller.Checkout(cart, shippingDetails); // Assert - check that the order has been passed on to the processor what.Verify(m => m.Process(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 we are passing a valid model to the view Assert.AreEqual(true, result.ViewData.ModelState.IsValid); }
public void CannotCheckoutInvalidShippingDetails() { // Arrange - create a mock order processor var mock = new Mock<IOrderProcessor>(); // Arrange - create a cart with an item var cart = new Cart(); cart.AddItem(new Product(), 1); // Arrange - create an instance of the controller var controller = new CartController(null, null); // Arrange - add an error to the model controller.ModelState.AddModelError("error", "error"); // Action - 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.CreateOrder(It.IsAny<Order>()), 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 CanClearContents() { // Arrange - create some test products var p1 = new Product {ProductId = 1, Name = "P1", Price = 100M}; var p2 = new Product {ProductId = 2, Name = "P2", Price = 50M}; // Arrange - create a new cart var target = new Cart(); // Arrange - add some items target.AddItem(p1, 1); target.AddItem(p2, 1); // Action - reset the cart target.Clear(); // Assert Assert.AreEqual(target.Lines.Count(), 0); }
public void CalculateCartTotal() { // Arrange - create some test products var p1 = new Product {ProductId = 1, Name = "P1", Price = 100M}; var p2 = new Product {ProductId = 2, Name = "P2", Price = 50M}; // Arrange - create a new cart var target = new Cart(); // Action target.AddItem(p1, 1); target.AddItem(p2, 1); target.AddItem(p1, 3); decimal result = target.ComputeTotalValue(); // Assert Assert.AreEqual(result, 450M); }