views:

111

answers:

1

Hello ,

I have a MyBO class which contains:

...
[RangeValidator(typeof(byte), "0", RangeBoundaryType.Inclusive, "20", RangeBoundaryType.Inclusive, Ruleset="validate_x")]
    public byte x
    {
        get;
        set;
    }

    [IgnoreNulls]
    [RangeValidator(typeof(byte), "0", RangeBoundaryType.Inclusive, "50", RangeBoundaryType.Inclusive, Ruleset = "validate_y")]
    public byte y
    {
        get;
        set;
    }

    [SelfValidation(Ruleset="validate_xy")]
    public void VerifyXY(ValidationResults results)
    {
        if (x < y)
        {
            results.AddResult(new ValidationResult("X cannot be < than Y!", this, "Verify", null, null));
        }
    }

The problem is that if in a test class i have:

    [TestMethod()]
    public void MyBOConstructorTest()
    {
        MyBO target = new MyBO() { x = 20, y = 23 };
        ValidationResults vr = Validation.Validate(target, "validate_xy");
        Assert.IsTrue(vr.IsValid);
    }

the test does not fail. Why? Because X is 20 and Y 23. So as you can see i'm using SelfValidation in the BO class.

Thanls

+2  A: 

The most logical explanation I can think of is that you didn't decorate your MyBO class with the [HasSelfValidation] attribute. Without this attribute the VerifyXY method will not be called:

[HasSelfValidation]
public class MyBO
{
    // implementation
}

I hope this helps.

Steven