public void ValidateCreateBookModel_WithTitleContainsSpaceOnly_ExpectValidationError() { //Setup a fake HttpRequest Mock <HttpContextBase> moqContext = new Mock <HttpContextBase>(); Mock <HttpRequestBase> moqRequest = new Mock <HttpRequestBase>(); Mock <HttpPostedFileBase> moqPostedFile = new Mock <HttpPostedFileBase>(); moqRequest.Setup(r => r.Files.Count).Returns(0); moqContext.Setup(x => x.Request).Returns(moqRequest.Object); //Arrange var controller = new CatalogController(); var book = new Book { Title = " ", Author = "David Chelimsky", Price = Convert.ToDecimal(17.50) }; controller.ControllerContext = new ControllerContext(moqContext.Object, new RouteData(), controller); var validationResults = TestModelHelper.ValidateModel(controller, book); //Act var viewResult = controller.Create(book) as ViewResult; //Assert Assert.IsNotNull(viewResult); //Assert.AreEqual(string.Empty, viewResult.ViewName); Assert.IsFalse(viewResult.ViewData.ModelState.IsValid); Assert.AreEqual(1, validationResults.Count); Assert.IsTrue(validationResults[0].ErrorMessage.Equals("The Title field is required.")); }
public void ValidateCreateBookModel_WithValidModel_ExpectValidNavigation() { //Setup a fake HttpRequest Mock <HttpContextBase> moqContext = new Mock <HttpContextBase>(); Mock <HttpRequestBase> moqRequest = new Mock <HttpRequestBase>(); Mock <HttpPostedFileBase> moqPostedFile = new Mock <HttpPostedFileBase>(); moqRequest.Setup(r => r.Files.Count).Returns(0); moqContext.Setup(x => x.Request).Returns(moqRequest.Object); //Arrange var controller = new CatalogController(); var book = new Book { Title = "The Cucumber Book", Author = "Matt Wynne and Aslak Hellesoy", Price = Convert.ToDecimal(18.00) }; controller.ControllerContext = new ControllerContext(moqContext.Object, new RouteData(), controller); var validationResults = TestModelHelper.ValidateModel(controller, book); //Act var redirectRoute = controller.Create(book) as RedirectToRouteResult; //Assert Assert.IsNotNull(redirectRoute); Assert.AreEqual("Index", redirectRoute.RouteValues["action"]); Assert.AreEqual("Catalog", redirectRoute.RouteValues["controller"]); Assert.AreEqual(0, validationResults.Count); }
public void ValidateCreateBookModel_WithTitleExceeds100Characters_ExpectValidationError() { //Setup a fake HttpRequest Mock <HttpContextBase> moqContext = new Mock <HttpContextBase>(); Mock <HttpRequestBase> moqRequest = new Mock <HttpRequestBase>(); Mock <HttpPostedFileBase> moqPostedFile = new Mock <HttpPostedFileBase>(); moqRequest.Setup(r => r.Files.Count).Returns(0); moqContext.Setup(x => x.Request).Returns(moqRequest.Object); //Arrange var controller = new CatalogController(); var book = new Book { Title = new string('*', 101), Author = "David Chelimsky", Price = Convert.ToDecimal(17.50) }; controller.ControllerContext = new ControllerContext(moqContext.Object, new RouteData(), controller); var validationResults = TestModelHelper.ValidateModel(controller, book); //Act var viewResult = controller.Create(book) as ViewResult; //Assert Assert.IsNotNull(viewResult); Assert.IsFalse(viewResult.ViewData.ModelState.IsValid); Assert.AreEqual(1, validationResults.Count); Assert.IsTrue(validationResults[0].ErrorMessage.Equals("The field Title must be a string with a maximum length of 100.")); }
/// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnSave_Click(object sender, DirectEventArgs e) { var model = new CatalogModel(hdfTableDM.Text) { Name = txtTenDM.Text }; CatalogController.Create(hdfTableDM.Text, model); RM.RegisterClientScriptBlock("add", "resetInputCategory();"); grpCategory.Reload(); }
/// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnSave_Click(object sender, DirectEventArgs e) { var model = new CatalogModel(hdfTableDM.Text) { Name = txtTenDM.Text }; //create CatalogController.Create(hdfTableDM.Text, model); //reload grid grpCategory.Reload(); ResetInputCategory(); }
/// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnSave_ClickGroup(object sender, DirectEventArgs e) { if (string.IsNullOrEmpty(hdfGroup.Text)) { Dialog.ShowError("Bạn chưa chọn loại danh mục"); return; } var model = new CatalogModel(hdfTableDM.Text) { Name = txtTenDMGroup.Text, Group = hdfGroup.Text }; CatalogController.Create(hdfCurrentCatalogName.Text, model); RM.RegisterClientScriptBlock("add", "resetInputCategoryGroup();"); grpCategoryGroup.Reload(); }
/// <summary> /// Insert or Update Catalog /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void InsertOrUpdate(object sender, DirectEventArgs e) { try { // init model var model = new CatalogModel(hdfObjName.Text) { EditedBy = CurrentUser.User.UserName }; // check id if (!string.IsNullOrEmpty(hdfId.Text) && Convert.ToInt32(hdfId.Text) > 0) { var result = CatalogController.GetById(hdfObjName.Text, Convert.ToInt32(hdfId.Text)); if (result != null) { model = result; } } // set new props for model model.Name = txtName.Text; model.Description = txtDescription.Text; model.Order = !string.IsNullOrEmpty(txtOrder.Text) ? Convert.ToInt32(txtOrder.Text) : 0; model.Group = hdfGroup.Text; if (Enum.TryParse(hdfStatus.Text, false, out CatalogStatus status)) { model.Status = status; } // if cat group is record status if (hdfCatalogGroup.Text == nameof(RecordStatus).ToLower()) { if (Enum.TryParse(hdfGroup.Text, false, out RecordStatus group)) { model.Group = ((int)group).ToString(); } } // create result model CatalogModel resultModel; // check model id if (model.Id > 0) { // update resultModel = CatalogController.Update(hdfObjName.Text, model); // log action SystemLogController.Create(new SystemLogModel(CurrentUser.User.UserName, "Danh mục {0}".FormatWith(hdfObjName.Text), SystemAction.Edit, "Sửa danh mục {0}".FormatWith(model.Name))); } else { // set created user model.CreatedBy = CurrentUser.User.UserName; // insert resultModel = CatalogController.Create(hdfObjName.Text, model); // log action SystemLogController.Create(new SystemLogModel(CurrentUser.User.UserName, "Danh mục {0}".FormatWith(hdfObjName.Text), SystemAction.Create, "Tạo danh mục {0}".FormatWith(model.Name))); } // show notification if (resultModel != null) { Dialog.ShowNotification("Lưu thành công"); // hide window wdSetting.Hide(); // reload data gpCatalog.Reload(); } else { Dialog.ShowNotification("Lưu không thành công, Tên đã tồn tại"); } } catch (Exception ex) { // show dilog Dialog.ShowError(ex); // log exception SystemLogController.Create(new SystemLogModel(CurrentUser.User.UserName, "Danh mục {0} - InsertOrUpdate".FormatWith(hdfObjName.Text), ex)); } }
public void NavigateCreateBook() { _result = _controller.Create(); }