Exemple #1
0
        public ActionResult Edit(Product product,HttpPostedFileBase image = null)
        {
            if(ModelState.IsValid)
            {
                if(image != null)
                {   //set to save image to DB

                    product.ImageMimeType = image.ContentType;
                    product.ImageData = new byte[image.ContentLength];
                    image.InputStream.Read(product.ImageData, 0, image.ContentLength);
                }
                if(product.ProductID != 0)
                {
                    repository.SaveProduct(product);
                }

                //saved msg 'til session end
                TempData["message"] = string.Format("{0} has been saved", product.Name);
                return RedirectToAction("Index");
            }
            else
            { //some thing wrong with data let user fix it
                return View(product);
            }
        }
Exemple #2
0
        public void Can_Retrieve_Image_Data()
        {
            // Arrange - create a Product with image data
            Product prod = new Product
            {
                ProductID = 2,
                Name = "Test",
                ImageData = new byte[] { },
                ImageMimeType = "image/png"
            };
            // Arrange - create the mock repository
            Mock<IProductsRepository> mock = new Mock<IProductsRepository>();
            mock.Setup(m => m.Products).Returns(new Product[] {
                new Product {ProductID = 1, Name = "P1"},
                prod,
                new Product {ProductID = 3, Name = "P3"}
                }.AsQueryable());
            // Arrange - create the controller
            ProductController 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();
 }
Exemple #4
0
 public void Add( Product product, int quantity )
 {
     if (_lines.Count(x=>x.Product.ProductID == product.ProductID) > 0) {
         _lines.Where(x=>x.Product.ProductID == product.ProductID).First().Quantity += quantity;
     }
     else {
         _lines.Add(new CartLine { Product = product, Quantity = quantity });
     }
 }
 public void EditTest_CanSaveValidChanges()
 {
     var mock = new Mock<IProductRepository>();
     var target = new AdminController(mock.Object);
     var product = new Product { Name = "Test" };
     ActionResult actual = target.Edit(product, null);
     ////检查是否调用了存储库
     mock.Verify(m => m.SaveProduct(product));
     ////检查方法的结果类型
     Assert.IsNotInstanceOfType(actual, typeof(ViewResult));
 }
 public ActionResult Edit(Product product)
 {
     if (ModelState.IsValid) {
         _repository.Save(product);
         TempData["message"] = product.Name + " has been saved";
         return RedirectToAction("Index");
     }
     else {
         return View(product);
     }
 }
 public void EditTest_CannotSaveInvalidChanges()
 {
     var mock = new Mock<IProductRepository>();
     var target = new AdminController(mock.Object);
     var product = new Product { Name = "Test" };
     target.ModelState.AddModelError("error", "error");
     ActionResult actual = target.Edit(product, null);
     ////检查是否调用了存储库
     mock.Verify(m => m.SaveProduct(It.IsAny<Product>()), Times.Never());
     ////检查方法的结果类型
     Assert.IsInstanceOfType(actual, typeof(ViewResult));
 }
        public void Save(Product product)
        {
            if (product.ProductID == 0) {
                productsTable.InsertOnSubmit(product);
            }
            else if (productsTable.GetOriginalEntityState(product) == null) {
                productsTable.Attach(product);
                productsTable.Context.Refresh(RefreshMode.KeepCurrentValues, product);
            }

            productsTable.Context.SubmitChanges();
        }
Exemple #9
0
        public void Calculate_Cart_Total()
        {
            Product p1 = new Product { ProductID = 1, Name = "P1", Price = 100M };
            Product p2 = new Product { ProductID = 2, Name = "P2", Price =50M};

            Cart target = new Cart();

            target.AddItem(p1, 2);
            target.AddItem(p2, 2);
            decimal result = target.ComputeTotalValue();

            Assert.AreEqual(result, 300M);
        }
Exemple #10
0
        public void AddItem(Product product, int quantity)
        {
            CartLine line = lineCollection.Where(p => p.Product.ProductID == product.ProductID).FirstOrDefault();

            if (line == null)
            {
                lineCollection.Add(new CartLine { Product = product, Quantity = quantity });
            }
            else
            {
                line.Quantity += quantity;
            }
        }
        public void CanClearContent()
        {
            Product product1 = new Product { ProductId = 1, Name = "Product1", Price = 100m };
            Product product2 = new Product { ProductId = 2, Name = "Product2", Price = 50m };

            Cart target = new Cart();
            target.AddItem(product1, 1);
            target.AddItem(product2, 1);

            target.Clear();

            Assert.AreEqual(target.Lines.Count(), 0);
        }
        public void CanAddNewLines()
        {
            Product product1 = new Product { ProductId = 1, Name = "Product1" };
            Product product2 = new Product { ProductId = 2, Name = "Product2" };

            Cart target = new Cart();
            target.AddItem(product1, 1);
            target.AddItem(product2, 1);
            CartLine[] result = target.Lines.ToArray();

            Assert.AreEqual(result.Length, 2);
            Assert.AreEqual(result[0].Product, product1);
            Assert.AreEqual(result[1].Product, product2);
        }
        public void CalcuateCartTotal()
        {
            Product product1 = new Product { ProductId = 1, Name = "Product1", Price = 100m };
            Product product2 = new Product { ProductId = 2, Name = "Product2", Price = 50m };

            Cart target = new Cart();
            target.AddItem(product1, 1);
            target.AddItem(product2, 1);
            target.AddItem(product1, 3);

            decimal actual = target.ComputeTotalValue();
            decimal expected = 450m;

            Assert.AreEqual(actual, expected);
        }
Exemple #14
0
       public void Can_Add_New_Lines()
        {
            Product p1 = new Product { ProductID = 1, Name = "P1" };
            Product p2 = new Product { ProductID = 2, Name = "P2" };

            Cart target = new Cart();

            target.AddItem(p1, 1);
            target.AddItem(p2, 1);
            CartLine[] results = target.Lines.ToArray();

            Assert.AreEqual(results.Length , 2);
            Assert.AreEqual(results[0].Product, p1);
            Assert.AreEqual(results[1].Product, p2);
        }
Exemple #15
0
        public void Can_Add_Quantity_For_Existing_Item()
        {
            Product p1 = new Product { ProductID = 1, Name = "P1" };
            Product p2 = new Product { ProductID = 2, Name = "P2" };

            Cart target = new Cart();

            target.AddItem(p1, 1);
            target.AddItem(p2, 1);
            target.AddItem(p1, 1);
            CartLine[] results = target.Lines.ToArray();

            Assert.AreEqual(results.Length, 2);
            Assert.AreEqual(results[0].Quantity, 2);
        }
Exemple #16
0
 public void Calculate_Cart_Total()
 {
     // Arrange - create some test products
     Product p1 = new Product { ProductID = 1, Name = "P1", Price = 100M };
     Product p2 = new Product { ProductID = 2, Name = "P2", Price = 50M };
     // Arrange - create a new cart
     Cart 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);
 }
        public void CanAddQuantityForExistingLines()
        {
            Product product1 = new Product { ProductId = 1, Name = "Product1" };
            Product product2 = new Product { ProductId = 2, Name = "Product2" };

            Cart target = new Cart();
            target.AddItem(product1, 1);
            target.AddItem(product2, 1);
            target.AddItem(product1, 10);

            CartLine[] result = target.Lines.OrderBy(c => c.Product.ProductId).ToArray();

            Assert.AreEqual(result.Length, 2);
            Assert.AreEqual(result[0].Quantity, 11);
            Assert.AreEqual(result[1].Quantity, 1);
        }
        public void Can_Delete_Valid_Products()
        {
            Product product = new Product { ProductID = 2, Name = "Test" };

            Mock<IProductsRepository> mock = new Mock<IProductsRepository>();
            mock.Setup(m => m.Products).Returns(new Product[]{
                new Product{ProductID = 1, Name = "P1"},
                new Product{ProductID = 3, Name = "P3"}
            });

            AdminController target = new AdminController(mock.Object);

            target.Delete(product.ProductID);

            mock.Verify(m => m.DeleteProduct(product.ProductID));
        }
        public void Cannot_Save_Invalid_Changes()
        {
            Mock<IProductsRepository> mock = new Mock<IProductsRepository>();

            AdminController target = new AdminController(mock.Object);

            Product product = new Product { Name = "Test" };

            target.ModelState.AddModelError("error", "error");

            ActionResult result = target.Edit(product);

            mock.Verify(m => m.SaveProduct(It.IsAny<Product>()), Times.Never());

            Assert.IsInstanceOfType(result, typeof(ViewResult));
        }
Exemple #20
0
 public void Can_Add_New_Lines()
 {
     // Arrange - create some test products
     Product p1 = new Product { ProductID = 1, Name = "P1" };
     Product p2 = new Product { ProductID = 2, Name = "P2" };
     // Arrange - create a new cart
     Cart 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);
 }
        public void can_add_products_to_cart()
        {
            // arrange
            IProductsRepository repository = Substitute.For<IProductsRepository>();
            Product someProduct = new Product { ProductID= 123 };
            repository.Products.Returns( new List<Product> { someProduct }.AsQueryable() );
            Cart cart = new Cart();

            CartController controller = new CartController(repository, null);

            // act
            controller.AddProduct(123, null, cart);

            // assert
            cart.Lines[0].Product.ShouldEqual(someProduct);
        }
        public void CanRemoveLine()
        {
            Product product1 = new Product { ProductId = 1, Name = "Product1" };
            Product product2 = new Product { ProductId = 2, Name = "Product2" };
            Product product3 = new Product { ProductId = 3, Name = "Product3" };

            Cart target = new Cart();
            target.AddItem(product1, 1);
            target.AddItem(product2, 3);
            target.AddItem(product3, 5);
            target.AddItem(product2, 1);

            target.RemoveLine(product2);

            Assert.AreEqual(target.Lines.Where(c => c.Product == product2).Count(), 0);
            Assert.AreEqual(target.Lines.Count(), 2);
        }
Exemple #23
0
 public void Can_Add_Quantity_For_Existing_Lines()
 {
     // Arrange - create some test products
     Product p1 = new Product { ProductID = 1, Name = "P1" };
     Product p2 = new Product { ProductID = 2, Name = "P2" };
     // Arrange - create a new cart
     Cart target = new Cart();
     // Act
     target.AddItem(p1, 4);
     target.AddItem(p2, 1);
     target.AddItem(p1, 3);
     CartLine[] results = target.Lines.OrderBy(c => c.Product.ProductID).ToArray();
     // Assert
     Assert.AreEqual(results.Length, 2);
     Assert.AreEqual(results[0].Quantity, 7);
     Assert.AreEqual(results[1].Quantity, 1);
 }
 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 View(product);
     }
 }
Exemple #25
0
        public void Can_Remove_Lines()
        {
            Product p1 = new Product { ProductID = 1, Name = "P1" };
            Product p2 = new Product { ProductID = 2, Name = "P2" };
            Product p3 = new Product { ProductID = 3, Name = "P3" };

            Cart target = new Cart();

            target.AddItem(p1, 3);
            target.AddItem(p2, 1);
            target.RemoveLine(p2);
            target.AddItem(p3, 3);

            Assert.AreEqual(target.Lines.Where(c => c.Product == p2).Count(), 0);
            Assert.AreEqual(target.Lines.Where(c => c.Product == p1).Count(), 1);

            Assert.AreEqual(target.Lines.Count(), 2);

        }
Exemple #26
0
 public void Can_Clear_Contents()
 {
     // Arrange - create some test products
     Product p1 = new Product
     {
         ProductID = 1,
         Name = "P1",
         Price = 100M
     };
     Product p2 = new Product { ProductID = 2, Name = "P2", Price = 50M };
     // Arrange - create a new cart
     Cart 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 SaveProduct(Product product)
 {
     if(product.ProductID == 0)
     {
         context.Products.Add(product);
     }else
     {
         Product dbEntry = context.Products.Find(product.ProductID);
         if(dbEntry != null)
         {
             dbEntry.Name = product.Name;
             dbEntry.Description = product.Description;
             dbEntry.Price = product.Price;
             dbEntry.Category = product.Category;
             dbEntry.ImageData = product.ImageData;
             dbEntry.ImageMimeType = product.ImageMimeType;
         }
     }
     context.SaveChanges();
 }
 public void Can_Retrieve_Image_Data()
 {
     var prod = new Product
     {
         ProductID = 2,
         Name = "Test",
         ImageData = new byte[] { },
         ImageMimeType = "image/png"
     };
     var mock = new Mock<IProductRepository>();
     mock.Setup(m => m.Products)
         .Returns(value: new Product[]
         {
             new Product{ProductID = 1,Name = "P1"},
             prod,
             new Product(){ProductID = 3,Name = "P3"}, 
         }.AsQueryable());
     var target = new ProductController(mock.Object);
     ActionResult result = target.GetImage(2);
     Assert.IsNotNull(result);
     Assert.IsInstanceOfType(result, typeof(FileResult));
     Assert.AreEqual(prod.ImageMimeType, ((FileResult)result).ContentType);
 }
Exemple #29
0
 public void Remove(Product product)
 {
     _lineCollection.RemoveAll(p => p.Product.ProductId == product.ProductId);
 }
        public void Can_Save_Valid_Changes()
        {
            Mock<IProductsRepository> mock = new Mock<IProductsRepository>();

            AdminController target = new AdminController(mock.Object);

            Product product = new Product { Name = "Test" };

            ActionResult result = target.Edit(product);

            mock.Verify(m => m.SaveProduct(product));
            Assert.IsNotInstanceOfType(result, typeof(ViewResult));
        }
Exemple #31
0
 public void RemoveLine(Product product)
 {
     lineCollection.RemoveAll(l => l.Product.ProductID == product.ProductID);
 }