public void TestCreateG() { var controller = new SANPHAMsController(); var result = controller.Create() as ViewResult; Assert.IsNotNull(result); }
public void TestCreateP() { var rand = new Random(); var sanpham = new SANPHAM { MANCC = rand.Next(1, 5), MAKM = 7, LOAISP = rand.Next(1, 5), KICHTHUOC = rand.NextDouble().ToString(), MAUSAC = rand.NextDouble().ToString(), SOLUONG = rand.Next(), THONGTIN = rand.NextDouble().ToString(), GIABAN = -rand.Next(), NGAYTHEM = DateTime.Now, NGAYCAPNHAT = DateTime.Now }; var controller = new SANPHAMsController(); var result0 = controller.Create(sanpham, null) as ViewResult; Assert.IsNotNull(result0); Assert.AreEqual("Gia ban nho hon 0", controller.ModelState["GIABAN"].Errors[0].ErrorMessage); sanpham.GIABAN = -sanpham.GIABAN; controller.ModelState.Clear(); result0 = controller.Create(sanpham, 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(sanpham, picture.Object) as RedirectToRouteResult; Assert.IsNotNull(result1); Assert.AreEqual("Index", result1.RouteValues["action"]); var db = new CsK24T25Entities(); var entity = db.SANPHAMs.SingleOrDefault(p => p.TENSP == sanpham.TENSP && p.THONGTIN == sanpham.THONGTIN); Assert.IsNotNull(entity); Assert.AreEqual(sanpham.GIABAN, entity.GIABAN); Assert.IsTrue(fileName.StartsWith("~/Upload/Product/")); Assert.IsTrue(fileName.EndsWith(entity.MASP.ToString())); } }