我在使用 [ObjectValidator] 时遇到问题。所以我有:
public class UserBO
{
public int ID
{
get;
set;
}
[NotNullValidator(MessageTemplate = "Can't be null!")]
[RegexValidator(@"[a-z]|[A-Z]|[0-9]*", MessageTemplate = "Must be valid!", Ruleset = "validate_username")]
[StringLengthValidator(5, RangeBoundaryType.Inclusive, 25, RangeBoundaryType.Inclusive, Ruleset = "validate_username")]
public string username
{
get;
set;
}
和另一个类:
public class PersonBO
{
public int ID
{
get;
set;
}
[NotNullValidator(MessageTemplate="Can't be null!")]
[ObjectValidator(MessageTemplate = "Must be valid!", Ruleset="validate_obj_user")]
public UserBO User
{
get;
set;
}
...
现在你能告诉我为什么下面的测试通过了吗?
[TestMethod()]
public void PersonBOConstructorTest()
{
PersonBO target = new PersonBO()
{
User = new UserBO
{
ID = 4,
username = "asd"
}
};
ValidationResults vr = Validation.Validate<PersonBO>(target, "validate_obj_user");
Assert.IsTrue(vr.IsValid);
}
这应该是无效的,因为:用户属性(UserBO 类型)包含用户名“asd”(3 个字符),我为它定义了一个 StringLengthValidator(5 到 25 个字符之间).. 那么为什么测试通过了?该对象无效,我无法理解。
谢谢。