Вопрос

i have a rule defined below where the PreferredAirlineCodes is a string[] type

RuleForEach(x => x.PreferredAirlineCodes).Length(2);

however when I ran the following test...

validator.ShouldHaveValidationErrorFor(x => x.PreferredAirlineCodes, new []{"CX", "3KK"});

the test failed... did i defined the rule wrongly?

Это было полезно?

Решение

Your rule is defined fine.

However the ShouldHaveValidationErrorFor is only meant for testing simple cases: to test simple property validation rules. So it is not supporting testing nested properties or arrays (see also this issue)

So you need to test this validation rule "manually":

var validator = new MyClassValidator();
var result = validator.Validate(
    new MyClass() { PreferredAirlineCodes = new[] {"CX", "3KK"}});
Assert.IsTrue( 
    result.Errors.Any(e => e.PropertyName.StartsWith("PreferredAirlineCodes")));
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top