开发者

ObjectValidator unit testing

开发者 https://www.devze.com 2022-12-20 04:11 出处:网络
I\'m having problems with [ObjectValidator]. So, i have: public class UserBO { public int ID { get; set; } [NotNullValidator(MessageTemplate = \"Can\'t be null!\")]

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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消