Test Validation Rules
- 3 minutes to read
The EasyTest functional testing framework allows you to test validation rules.
To see step-by-step testing instructions for beginners, refer to the Test an Action topic.
Suppose you have an Employee business class, which declares the Name and Age properties. The properties have the following requirements:
- Name. Each Employee object must have a non-empty Name. To do this, apply the RuleRequiredFieldAttribute to the Name property.
- Age. The Age value must be 18 or higher. Decorate the Age property with the RuleValueComparisonAttribute.
The following code snippet demonstrates how to apply these validation rules:
using DevExpress.Persistent.Base;
using DevExpress.Persistent.BaseImpl.EF;
using DevExpress.Persistent.Validation;
using System.ComponentModel.DataAnnotations;
// ...
[DefaultClassOptions,ImageName("BO_Employee")]
public class Employee : BaseObject {
[RuleRequiredField("EmployeeIsAdult", DefaultContexts.Save)]
public virtual string Name { get; set; }
[RuleValueComparison("EmployeeNameIsRequired", DefaultContexts.Save, ValueComparisonType.GreaterThanOrEqual, 18)]
public virtual int Age { get; set; }
}
// Make sure that you use options.UseChangeTrackingProxies() in your DbContext settings.
CheckValidationResult Command
In a Windows Forms application, the popup window is displayed when a validation error occurs. In an ASP.NET Web Forms and Blazor application, the validation results are displayed on the current page using the ErrorInfoControl (see the Error Messages on the Current Page (ErrorInfoControl) topic section). To test all platforms in a single test script, you can use the CheckValidationResult EasyTest command. The image below illustrates the meaning of the command’s parameters on WinForms and ASP.NET Web Forms platforms.
To test the Employee object’s validation rules, you can use the following EasyTest script.
;#DropDB MySolutionEasyTest
#Application MySolutionWin
#Application MySolutionWeb
#Application MySolutionBlazor
;Create a new Employee:
*Action Navigation(Employee)
*Action New
;Save it with initial property values (empty Name and zero Age):
*Action Save
;Test the validation result that is displayed when saving:
*CheckValidationResult
Message = Data Validation Error: *
Info = "Name" must not be empty.
Info = "Age" must be greater than or equal to "18".
;Close the error message:
#IfDef MySolutionWin
*Action Close
#EndIf
;Specify a valid Name and invalid Age:
*FillForm
Name = Mary Tellitson
Age = 17
;Save an Employee with invalid Age:
*Action Save
;Test the validation result that is displayed when saving:
*CheckValidationResult
Message = Data Validation Error: *
Info = "Age" must be greater than or equal to "18".
;Close the error message:
#IfDef MySolutionWin
*Action Close
#EndIf
;Specify a valid Age:
*FillForm
Age = 18
;Save a valid Employee:
*Action Save
;Check that an error is not displayed:
!CheckValidationResult
Message = ?*