public void TestCreateP() { var rand = new Random(); var sach = new SACH { TENSACH = rand.NextDouble().ToString(), THONGTIN = rand.NextDouble().ToString(), GIASACH = -rand.Next() }; var controller = new SACHesController(); var result0 = controller.Create(sach, null) as ViewResult; Assert.IsNotNull(result0); Assert.AreEqual("Price is less than Zero", controller.ModelState["Price"].Errors[0].ErrorMessage); sach.GIASACH = -sach.GIASACH; controller.ModelState.Clear(); result0 = controller.Create(sach, 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(sach, picture.Object) as RedirectToRouteResult; Assert.IsNotNull(result1); Assert.AreEqual("Index", result1.RouteValues["action"]); var db = new CsK24_BookStoreEntities(); var entity = db.SACHes.SingleOrDefault(p => p.TENSACH == sach.TENSACH && p.THONGTIN == sach.THONGTIN); Assert.IsNotNull(entity); Assert.AreEqual(sach.GIASACH, entity.GIASACH); Assert.IsTrue(fileName.StartsWith("~/Upload/Sach/")); Assert.IsTrue(fileName.EndsWith(entity.MASACH.ToString())); } }
public void TestCreateG() { var controller = new SACHesController(); var result = controller.Create() as ViewResult; Assert.IsNotNull(result); }