Skip to main content

Test Validation Rules

  • 3 minutes to read

The EasyTest functional testing framework allows you to test validation rules.

Tip

A complete sample project is available in the DevExpress Code Examples database at https://supportcenter.devexpress.com/ticket/details/e2473/how-to-test-validation-rules.

To see beginner’s step-by-step testing instructions, 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:

The following code snippet demonstrates how to apply these validation rules:

using DevExpress.Persistent.Base;
using DevExpress.Persistent.BaseImpl;
using DevExpress.Persistent.Validation;
using DevExpress.Xpo;
// ...
[DefaultClassOptions, ImageName("BO_Employee")]
public class Employee : BaseObject {
    public Employee(Session session) : base(session) { }
    private string name;
    [RuleRequiredField("EmployeeIsAdult", DefaultContexts.Save)]
    public string Name {
        get => name;
        set => SetPropertyValue(nameof(Name), ref name, value);
    }
    private int age;
    [RuleValueComparison("EmployeeNameIsRequired", DefaultContexts.Save, ValueComparisonType.GreaterThanOrEqual, 18)]
    public int Age {
        get => age;
        set => SetPropertyValue(nameof(Age), ref age, value);
    }
}

CheckValidationResult Command

In 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 in WinForms and ASP.NET Web Forms platforms.

EasyTest_CheckValidationResult

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 = ?*
See Also