ObjectValidator 单元测试

发布于 2024-08-21 09:21:03 字数 1428 浏览 7 评论 0原文

我在使用 [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 个字符之间)..那么为什么测试通过呢?该对象无效 我无法理解。

谢谢。

I'm having problems with [ObjectValidator]. So, i have:

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;
    }

and another class:

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;
    }
...

Now can you tell me why the following test passes?

[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);
    }

This should not be valid, because: User attribute (of UserBO type) contains username "asd" (3 characters), and i defined for it a StringLengthValidator (between 5 and 25 characters).. so why the test passes? that object is not valid
I can't understand.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天赋异禀 2024-08-28 09:21:03

如果您想要应用默认集以外的规则集,则必须在 ObjectValidator 中指定规则集。

[ObjectValidator("validate_username", MessageTemplate = "Must be valid!", Ruleset = "validate_obj_user")]

上述内容应该适用于这种特定情况。或者,您可以从字符串长度验证器中删除规则集参数,以将其保留在默认集中。

You have to specify a ruleset in your ObjectValidator if you want rules from a set other than the default set applied.

[ObjectValidator("validate_username", MessageTemplate = "Must be valid!", Ruleset = "validate_obj_user")]

The above should work in this specific case. Alternatively, you could remove the ruleset parameter from the string length validator to leave it in the default set.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文