Skip to content

2. Configuration creation

Michał Jankowski edited this page Jul 17, 2020 · 13 revisions

FluentValidation.Validators.UnitTestExtension allows you to build test configuration for FlunetValidation rules. You can prepare test configuration in two ways:

  • by using BaseVerifiersSetComposer
  • creation array of IValidatorVerifier

I recommend to use first apporach beacuse it is more comfortable than second one.

BaseVerifiersSetComposer

BaseVerifiersSetComposer is class that provide generic methods for configuration construction. You cannot create instance of this class directly. Instead of constructir you should use static method Build(). This method should be used each time when you need to create new configuration.

After that you can use one of following methods for adding validation verifiers:

  • AddPropertyValidatorVerifier<T>() - T should implement IPropertyValidator
  • AddPropertyValidatorVerifier<T>(object valueToCompare) - T should implement IComparisonValidator
  • AddPropertyValidatorVerifier<T>(int min, int max) - T should implement ILengthValidator
  • RegularExpressionValidatorVerifier - T should implement IRegularExpressionValidator
  • ScalePrecisionValidatorVerifier<T> - T should inherit from ScalePrecisionValidator
  • AddChildValidatorVerifier<T, TProperty>()
  • AddVerifier(IValidatorVerifier ruleVerifier) - allows usage of your own implementer Verifier
  • AddPlaceholderVerifier() - insterts dummy verifier that does not check anything

As <T> you must insert one of validators provided by FluentValidation or your validator that you have implemented by yourself. Togheter with that you are able to instert also parameters when provided validator type needs it.

And finally you must execute Create() method. It transfomrs configuration into IValidatorVerifier array. Such created configuration can be used in unit tests.

Example
BaseVerifiersSetComposer.Build()
    .AddPropertyValidatorVerifier<NotNullValidator>()
    .AddPropertyValidatorVerifier<NotEmptyValidator>()
    .AddPropertyValidatorVerifier<LengthValidator>(0, 20)
    .Create()

Manual configuation

Other option is to create configuration manually by providing array of objects that implements IValidatorVerifier interface.

List of already implemented verifiers can be found under following link: Validator verifiers.

Example
new IValidatorVerifier[]
{
    new TypeValidatorVerifier<NotNullValidator>(),
    new TypeValidatorVerifier<NotEmptyValidator>(),
    new LengthValidatorVerifier<LengthValidator>(0, 20)
});

Clone this wiki locally