문제

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