public void Validate_RequiredFields_Invalid() { // Arrange Customer objCustomer = new Customer(); objCustomer.Id = null; objCustomer.CustomerCode = null; objCustomer.CustomerName = null; objCustomer.NAS = null; objCustomer.Amount = null; objCustomer.BirthDate = null; objCustomer.OtherDate = null; ValidationContext context = new ValidationContext(objCustomer, null, null); List<ValidationResult> results = new List<ValidationResult>(); // Act bool isModelStateValide = Validator.TryValidateObject(objCustomer, context, results, true); // Assert Assert.False(isModelStateValide); Assert.Equal(3, results.Count); Assert.Contains("is required", results[0].ErrorMessage); Assert.Contains("is required", results[1].ErrorMessage); Assert.Contains("is required", results[2].ErrorMessage); }
public CustomerController() { Customer obj1 = new Customer(); obj1.Id = 12; obj1.CustomerCode = "1001"; obj1.CustomerName = "Asterix"; obj1.NAS = "046454286"; obj1.Amount = 90.34M; obj1.BirthDate = new DateTime(1008, 11, 01); Customers.Add(obj1); obj1 = new Customer(); obj1.Id = 11; obj1.CustomerCode = "1002"; obj1.CustomerName = "Obelix"; obj1.NAS = "046454286"; obj1.Amount = 91.00M; obj1.BirthDate = new DateTime(972, 10, 05); Customers.Add(obj1); obj1 = new Customer(); obj1.Id = 0; obj1.CustomerCode = "0000"; obj1.NAS = "000000000"; obj1.CustomerName = String.Empty; obj1.Amount = 0.00M; obj1.BirthDate = new DateTime(1, 1, 1); Customers.Add(obj1); }
// GET: /Customers/Json/ItsAPopup public ActionResult ItsAPopup() { Customer obj = new Customer(); obj.Id = 12; obj.CustomerCode = "1001"; obj.CustomerName = "Asterix"; obj.NAS = "046454286"; obj.Amount = 90.34M; obj.BirthDate = new DateTime(1008, 11, 01); return PartialView("_Popup", obj); }
public ActionResult DisplayCustomer(Customer obj) { if (ModelState.IsValid) { return View("DisplayCustomer", obj); } else { ModelState.AddModelError("Customer Form", "There are errors in the form, please correct."); return View("CustomerForm", obj); } }
public void DisplayCustomer_Success() { // Arrange Customer objCustomer = new Customer(); objCustomer.Id = 10; objCustomer.CustomerCode = "1001"; objCustomer.CustomerName = "Asterix"; objCustomer.NAS = "046454286"; objCustomer.Amount = 90.10m; objCustomer.BirthDate = null; objCustomer.OtherDate = null; CustomerController controller = new CustomerController(); // Act ViewResult result = controller.DisplayCustomer(objCustomer) as ViewResult; // Assert Assert.Equal("DisplayCustomer", result.ViewName); }
public void Validate_NAS_Invalid() { // Arrange Customer objCustomer = new Customer(); objCustomer.Id = 10; objCustomer.CustomerCode = "1001"; objCustomer.CustomerName = "Asterix"; objCustomer.NAS = "123456789"; objCustomer.Amount = 90.10m; objCustomer.BirthDate = null; objCustomer.OtherDate = null; ValidationContext context = new ValidationContext(objCustomer, null, null); List<ValidationResult> results = new List<ValidationResult>(); // Act bool isModelStateValide = Validator.TryValidateObject(objCustomer, context, results, true); // Assert Assert.False(isModelStateValide); Assert.Equal(1, results.Count); Assert.Contains("is invalid", results[0].ErrorMessage); }
// GET: /Customers/Json/getJson public JsonResult getJson() { Customer obj = new Customer(); obj.CustomerCode = "c001"; return Json(obj/*, JsonRequestBehavior.AllowGet*/); }
public void Validate_StringLengths_Invalid() { // Arrange Customer objCustomer = new Customer(); objCustomer.Id = 10; objCustomer.CustomerCode = "12345"; objCustomer.CustomerName = "12345678901234567890123456789012345678901"; objCustomer.NAS = null; objCustomer.Amount = 90.10m; objCustomer.BirthDate = null; objCustomer.OtherDate = null; ValidationContext context = new ValidationContext(objCustomer, null, null); List<ValidationResult> results = new List<ValidationResult>(); // Act bool isModelStateValide = Validator.TryValidateObject(objCustomer, context, results, true); // Assert Assert.False(isModelStateValide); Assert.Equal(2, results.Count); Assert.Contains("must be a string with a maximum length of", results[0].ErrorMessage); Assert.Contains("must be a string with a maximum length of", results[1].ErrorMessage); }
// GET: /Customers/Customer/IsAmountValide public JsonResult IsAmountValide(Customer obj) { return (obj.NAS == null || obj.Amount > 0) ? Json(true/*, JsonRequestBehavior.AllowGet*/) : Json(false/*, JsonRequestBehavior.AllowGet*/); }