public void TestCreate() { var session = new MockHttpSession(); var context = new Mock <HttpContextBase>(); context.Setup(c => c.Session).Returns(session); var controller = new ShoppingCartController(); controller.ControllerContext = new ControllerContext(context.Object, new RouteData(), controller); var db = new CS4PEntities(); var product = db.Products.First(); var result = controller.Create(product.id, 2) as RedirectToRouteResult; Assert.IsNotNull(result); Assert.AreEqual("Index", result.RouteValues["action"]); var shoppingCart = session["ShoppingCart"] as List <BillDetail>; Assert.IsNotNull(shoppingCart); Assert.AreEqual(1, shoppingCart.Count); Assert.AreEqual(product.id, shoppingCart.First().Product.id); Assert.AreEqual(2, shoppingCart.First().Quantity); }
public void TestEditP()//Của thầy { var db = new CS4PEntities(); var model = new BubleTea { id = db.BubleTeas.AsNoTracking().First().id, Name = "Hoa huong duong", Topping = "tran chau, banh flan", Price = 0 }; var controller = new VLTeaController(); using (var scope = new TransactionScope()) { var result = controller.Edit(model); var view = result as ViewResult; Assert.IsNotNull(view); Assert.IsInstanceOfType(view.Model, typeof(BubleTea)); Assert.AreEqual(VLTError.PRICE_LESS_0, controller.ViewData.ModelState["Price"].Errors[0].ErrorMessage); model.Price = 26000; controller = new VLTeaController(); result = controller.Edit(model); var redirect = result as RedirectToRouteResult; Assert.IsNotNull(redirect); Assert.AreEqual("Index", redirect.RouteValues["action"]); var item = db.BubleTeas.Find(model.id); Assert.IsNotNull(item); Assert.AreEqual(model.Name, item.Name); Assert.AreEqual(model.Topping, item.Topping); Assert.AreEqual(model.Price, item.Price); } }
public void TestIndex() { var controller = new BangSanPhamController(); var result = controller.Index() as ViewResult; var db = new CS4PEntities(); Assert.IsNotNull(result); Assert.IsInstanceOfType(result.Model, typeof(List <BangSanPham>)); Assert.AreEqual(db.BangSanPhams.Count(), ((List <BangSanPham>)result.Model).Count); }
public void TestCreateP() { var rand = new Random(); var product = new Product { Name = rand.NextDouble().ToString(), Description = rand.NextDouble().ToString(), Price = -rand.Next() }; var controller = new ProductsController(); var result0 = controller.Create(product, null) as ViewResult; Assert.IsNotNull(result0); Assert.AreEqual("Price is less than Zero", controller.ModelState["Price"].Errors[0].ErrorMessage); product.Price = -product.Price; controller.ModelState.Clear(); result0 = controller.Create(product, null) as ViewResult; Assert.IsNotNull(result0); Assert.AreEqual("Picture not found!", controller.ModelState[""].Errors[0].ErrorMessage); var picture = new Mock <HttpPostedFileBase>(); var server = new Mock <HttpServerUtilityBase>(); var context = new Mock <HttpContextBase>(); context.Setup(c => c.Server).Returns(server.Object); controller.ControllerContext = new ControllerContext(context.Object, new System.Web.Routing.RouteData(), controller); var fileName = String.Empty; server.Setup(s => s.MapPath(It.IsAny <string>())).Returns <string>(s => s); picture.Setup(p => p.SaveAs(It.IsAny <string>())).Callback <string>(s => fileName = s); using (var scope = new TransactionScope()) { controller.ModelState.Clear(); var result1 = controller.Create(product, picture.Object) as RedirectToRouteResult; Assert.IsNotNull(result1); Assert.AreEqual("Index", result1.RouteValues["action"]); var db = new CS4PEntities(); var entity = db.Products.SingleOrDefault(p => p.Name == product.Name && p.Description == product.Description); Assert.IsNotNull(entity); Assert.AreEqual(product.Price, entity.Price); Assert.IsTrue(fileName.StartsWith("~/Upload/Products/")); Assert.IsTrue(fileName.EndsWith(entity.id.ToString())); } }
public void TestIndex() { var db = new CS4PEntities(); var controller = new VLTeaController(); var result = controller.Index(); var view = result as ViewResult; Assert.IsNotNull(view); var model = view.Model as List <BubleTea>; Assert.IsNotNull(model); Assert.AreEqual(db.BubleTeas.Count(), model.Count); }
public void TestIndex2() { var controller = new ProductsController(); var result = controller.Index2() as ViewResult; Assert.IsNotNull(result); var model = result.Model as List <Product>; Assert.IsNotNull(model); var db = new CS4PEntities(); Assert.AreEqual(db.Products.Count(), model.Count); }
public void TestIndex() { var session = new MockHttpSession(); var context = new Mock <HttpContextBase>(); context.Setup(c => c.Session).Returns(session); var controller = new ShoppingCartController(); controller.ControllerContext = new ControllerContext(context.Object, new RouteData(), controller); session["ShoppingCart"] = null; var result = controller.Index() as ViewResult; Assert.IsNotNull(result); var model = result.Model as List <BillDetail>; Assert.IsNotNull(model); Assert.AreEqual(0, model.Count); var db = new CS4PEntities(); var product = db.Products.First(); var shoppingCart = new List <BillDetail>(); shoppingCart.Add(new BillDetail { Product = product, Quantity = 1 }); var billDetail = new BillDetail(); billDetail.Product = product; billDetail.Quantity = 2; shoppingCart.Add(billDetail); session["ShoppingCart"] = shoppingCart; result = controller.Index() as ViewResult; Assert.IsNotNull(result); model = result.Model as List <BillDetail>; Assert.IsNotNull(model); Assert.AreEqual(1, model.Count); Assert.AreEqual(product.id, model.First().Product.id); Assert.AreEqual(3, model.First().Quantity); }
public void TestDelete() { var db = new CS4PEntities(); var controller = new VLTeaController(); using (var scope = new TransactionScope()) { var result = controller.Delete(0); Assert.IsInstanceOfType(result, typeof(HttpNotFoundResult)); var model = db.BubleTeas.AsNoTracking().First(); result = controller.Delete(model.id); var redirect = result as RedirectToRouteResult; Assert.IsNotNull(redirect); Assert.AreEqual("Index", redirect.RouteValues["action"]); var item = db.BubleTeas.Find(model.id); Assert.IsNull(item); } }
public void TestDeleteP() { var db = new CS4PEntities(); var product = db.Products.AsNoTracking().First(); var controller = new ProductsController(); var context = new Mock <HttpContextBase>(); var server = new Mock <HttpServerUtilityBase>(); context.Setup(c => c.Server).Returns(server.Object); controller.ControllerContext = new ControllerContext(context.Object, new System.Web.Routing.RouteData(), controller); var filePath = String.Empty; var tempDir = System.IO.Path.GetTempFileName(); server.Setup(s => s.MapPath(It.IsAny <string>())).Returns <string>(s => { filePath = s; return(tempDir); }); using (var scope = new TransactionScope()) { System.IO.File.Delete(tempDir); System.IO.Directory.CreateDirectory(tempDir); tempDir = tempDir + "/"; System.IO.File.Create(tempDir + product.id).Close(); Assert.IsTrue(System.IO.File.Exists(tempDir + product.id)); var result = controller.DeleteConfirmed(product.id) as RedirectToRouteResult; Assert.IsNotNull(result); Assert.AreEqual("Index", result.RouteValues["action"]); var entity = db.Products.Find(product.id); Assert.IsNull(entity); Assert.AreEqual("~/Upload/Products/", filePath); Assert.IsFalse(System.IO.File.Exists(tempDir + product.id)); } }
public void TestDetails() { var controller = new ProductsController(); var result0 = controller.Details(0) as HttpNotFoundResult; Assert.IsNotNull(result0); var db = new CS4PEntities(); var product = db.Products.First(); var result1 = controller.Details(product.id) as ViewResult; Assert.IsNotNull(result1); var model = result1.Model as Product; Assert.IsNotNull(model); Assert.AreEqual(product.Name, model.Name); Assert.AreEqual(product.Price, model.Price); Assert.AreEqual(product.Description, model.Description); }
public void TestSearch() { var db = new CS4PEntities(); var products = db.Products.ToList(); var keyword = products.First().Name.Split().First(); products = products.Where(p => p.Name.ToLower().Contains(keyword.ToLower())).ToList(); var controller = new ProductsController(); var result = controller.Search(keyword) as ViewResult; Assert.IsNotNull(result); var model = result.Model as List <Product>; Assert.IsNotNull(model); Assert.AreEqual("Index2", result.ViewName); Assert.AreEqual(products.Count(), model.Count); Assert.AreEqual(keyword, result.ViewData["Keyword"]); }
public void TestCreate2() { var controller = new BangSanPhamController(); var context = new Mock <HttpContextBase>(); var request = new Mock <HttpRequestBase>(); var files = new Mock <HttpFileCollectionBase>(); var file = new Mock <HttpPostedFileBase>(); context.Setup(c => c.Request).Returns(request.Object); request.Setup(r => r.Files).Returns(files.Object); files.Setup(f => f["HinhAnh"]).Returns(file.Object); file.Setup(f => f.ContentLength).Returns(1); context.Setup(c => c.Server.MapPath("~/App_Data")).Returns("~/App_Data"); controller.ControllerContext = new ControllerContext(context.Object, new RouteData(), controller); var db = new CS4PEntities(); var model = new BangSanPham(); model.Loai_id = db.LoaiSanPhams.First().id; model.TenSP = "TenSP"; model.MaSP = "MaSP"; model.GiaGoc = 123; model.GiaBan = 456; model.GiaGop = 789; model.SoLuongTon = 10; using (var scope = new TransactionScope()) { var result0 = controller.Create(model) as RedirectToRouteResult; Assert.IsNotNull(result0); file.Verify(f => f.SaveAs(It.Is <string>(s => s.StartsWith("~/App_Data/")))); Assert.AreEqual("Index", result0.RouteValues["action"]); file.Setup(f => f.ContentLength).Returns(0); var result1 = controller.Create(model) as ViewResult; Assert.IsNotNull(result1); Assert.IsInstanceOfType(result1.ViewData["Loai_id"], typeof(SelectList)); } }
public void TestEditG() { var db = new CS4PEntities(); var item = db.BubleTeas.First(); var controller = new VLTeaController(); var result = controller.Edit(0); Assert.IsInstanceOfType(result, typeof(HttpNotFoundResult)); result = controller.Edit(item.id); var view = result as ViewResult; Assert.IsNotNull(view); var model = view.Model as BubleTea; Assert.IsNotNull(model); Assert.AreEqual(item.id, model.id); Assert.AreEqual(item.Name, model.Name); Assert.AreEqual(item.Topping, model.Topping); Assert.AreEqual(item.Price, model.Price); }