Beispiel #1
0
 public void AddItem(Product product, int quantity)
 {
     CartLine line = this.lineCollection.Where(p => p.Product.ProductID == product.ProductID).FirstOrDefault();
     if (line == null)
     {
         this.lineCollection.Add(new CartLine { Product = product, Quantity = quantity });
     }
     else
     {
         line.Quantity += quantity;
     }
 }
 public void SaveProduct(Product product)
 {
     if (product.ProductID == 0)
     {
         context.Products.Add(product);
     }
     else
     {
         context.Entry(product).State = System.Data.EntityState.Modified;
     }
     context.SaveChanges();
 }
Beispiel #3
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();

            // Act
            target.AddItem(p1, 1);
            target.AddItem(p2, 1);
            target.AddItem(p1, 3);
            decimal result = target.ComputeTotalValue();

            // Assert
            Assert.AreEqual(result, 450M);
        }
Beispiel #4
0
 public ActionResult Edit(Product product, HttpPostedFileBase image)
 {
     if (ModelState.IsValid)
     {
         if (image != null)
         {
             product.ImageMimeType = image.ContentType;
             product.ImageData = new byte[image.ContentLength];
             image.InputStream.Read(product.ImageData, 0, image.ContentLength);
         }
         repository.SaveProduct(product);
         TempData["message"] = string.Format("{0} has been saved", product.Name);
         return RedirectToAction("Index");
     }
     else
     {
         return this.View(product);
     }
 }
Beispiel #5
0
        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();

            // Act
            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);
        }
Beispiel #6
0
        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();

            // Act
            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 void CanDeleteValidProducts()
        {
            // Arrange - create a Product
            var prod = new Product { ProductID = 2, Name = "Test" };

            // Arrange - create the mock repository
            var mock = new Mock<IProductRepository>();
            mock.Setup(m => m.Products).Returns(
                new[] { new Product { ProductID = 1, Name = "P1" }, prod, new Product { ProductID = 3, Name = "P3" }, }.
                    AsQueryable());

            // Arrange - create the controller
            var target = new AdminController(mock.Object);

            // Act - delete the product
            target.Delete(prod.ProductID);

            // Assert - ensure that the repository delete method was
            // called with the correct Product
            mock.Verify(m => m.DeleteProduct(prod));
        }
Beispiel #8
0
 public void RemoveLine(Product product)
 {
     this.lineCollection.RemoveAll(l => l.Product.ProductID == product.ProductID);
 }
Beispiel #9
0
        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);

            // Act
            target.RemoveLine(p2);

            // Assert
            Assert.AreEqual(target.Lines.Count(c => c.Product == p2), 0);
            Assert.AreEqual(target.Lines.Count(), 2);
        }
Beispiel #10
0
        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);

            // Act - reset the cart
            target.Clear();

            // Assert
            Assert.AreEqual(target.Lines.Count(), 0);
        }
        public void CanRetrieveImageData()
        {
            // Arrange - create a Product with image data
            var prod = new Product
                {
                   ProductID = 2, Name = "Test", ImageData = new byte[] { }, ImageMimeType = "image/png"
                };

            // Arrange - create the mock repository
            var mock = new Mock<IProductRepository>();
            mock.Setup(m => m.Products).Returns(
                new[] { new Product { ProductID = 1, Name = "P1" }, prod, new Product { ProductID = 3, Name = "P3" } }.
                    AsQueryable());

            // Arrange - create the controller
            var target = new ProductController(mock.Object);

            // Act - call the GetImage action method
            ActionResult result = target.GetImage(2);

            // Assert
            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(FileResult));
            Assert.AreEqual(prod.ImageMimeType, ((FileResult)result).ContentType);
        }
 public void DeleteProduct(Product product)
 {
     context.Products.Remove(product);
     context.SaveChanges();
 }
        public void CanSaveValidChanges()
        {
            // Arrange - create mock repository
            var mock = new Mock<IProductRepository>();

            // Arrange - create the controller
            var target = new AdminController(mock.Object);

            // Arrange - create a product
            var product = new Product { Name = "Test" };

            // Act - try to save the product
            ActionResult result = target.Edit(product, null);

            // Assert - check that the repository was called
            mock.Verify(m => m.SaveProduct(product));

            // Assert - check the method result type
            Assert.IsNotInstanceOfType(result, typeof(ViewResult));
        }
        public void CannotSaveInvalidChanges()
        {
            // Arrange - create mock repository
            var mock = new Mock<IProductRepository>();

            // Arrange - create the controller
            var target = new AdminController(mock.Object);

            // Arrange - create a product
            var product = new Product { Name = "Test" };

            // Arrange - add an error to the model state
            target.ModelState.AddModelError("error", "error");

            // Act - try to save the product
            ActionResult result = target.Edit(product, null);

            // Assert - check that the repository was not called
            mock.Verify(m => m.SaveProduct(It.IsAny<Product>()), Times.Never());

            // Assert - check the method result type
            Assert.IsInstanceOfType(result, typeof(ViewResult));
        }