How to invalidate Dependancy rule for Silverlight unit test?

How to invalidate Dependancy rule for Silverlight unit test?

Old forum URL: forums.lhotka.net/forums/t/9573.aspx


rault posted on Friday, September 24, 2010

Has anyone tried setting a Dependency property in your business rules and then tried to unit test the rule? I'm having trouble figuring this out because we already have a rule that the dependency is triggering.

What I need to do in my Silverlight unit test is disable the first rule, set the first property so that it violates the rule, then enable the first rule again without running it. After that I can set my second property that has the first property as a dependency. Since the dependency exists it should trigger the rules for the first property proving that the first properties value is invalid.

Like this in the BO class:

public static PropertyInfo<int> PropOneProperty = RegisterProperty<int>(c => c.PropOne);
public int PropOne
{
    get { return GetProperty(PropOneProperty); }
    private set { SetProperty(PropOneProperty, value); }
}

public static PropertyInfo<int> PropTwoProperty = RegisterProperty<int>(c => c.PropTwo);
public int PropTwo
{
    get { return GetProperty(PropTwoProperty); }
    private set { SetProperty(PropTwoProperty, value); }
}

protected override void AddBusinessRules()
{
    base.AddBusinessRules();
    BusinessRules.AddRule(new IsPropOneDuplicate(PropOne)); //just checks to see if it already exists in the mock data
    BusinessRules.AddRule(new Csla.Rules.CommonRules.Dependency(PropOne, PropTwo)); //if PropTwo gets changed then we run the rules for PropOne as well
}

And like this in the Silverlight unit test:

[TestMethod, Asynchronous]
public void DependancyTest()
{
    Project.ViewModels.EditViewModel viewModel = null;
    Login();
    EnqueueConditional(() => _loginComplete); //class bool set from Login method
    EnqueueCallback(() =>
    {
        // construct from mock data
        viewModel = new Project.ViewModels.EditViewModel(1);
    });
    EnqueueConditional(() => !viewModel.IsBusy && viewModel.Model != null);
    EnqueueCallback(() =>
    {
                /*
                    1.    Create a valid object
                    2.    Turn off the duplicate PropOne rule temporarily (how?)
                    3.    Set its PropOne duplicate
                    4.    Enable the duplicate rule but don’t run it (how?)
                    5.    Set the PropTwo to a new ID, which should then run the duplicate PropOne rule invalidating the object
                 */
        //set property, should fire Dependancy rule
        //<-- turn off duplicate rule here
        viewModel.Model.PropOne = "Test Name"; //a duplicate name in the mock data for PropOne
        //<-- turn on duplicate rule here but don't run it
        viewModel.Model.PropTwo = 1; //a new id for PropTwo <-- dependency rule should fire and make this object invalid because the PropOne is duplicate
    });
    EnqueueConditional(() => !viewModel.IsBusy && viewModel.Model.PropOne == "Test Name");
    EnqueueCallback(() =>
    {
        string brokenRules = viewModel.Model.BrokenRulesCollection.ToString();

        //it's not valid
        Assert.IsFalse(viewModel.Model.IsValid);
    });
    EnqueueTestComplete();
}

JonnyBee replied on Friday, September 24, 2010

Hi,

You have the parameters in the wrong order for Dependency rule:

    BusinessRules.AddRule(new Csla.Rules.CommonRules.Dependency(PropOne, PropTwo)); //if PropTwo gets changed then we run the rules for PropOne as wel

This line actually means:
When PropOne changes the rules for PropTwo must also be run

You can even specify multiple properties like this
new CommonRules.Dependency(PropOne, PropTwo, PropThree, PropFour));

result:  When PropOne changes - check rules for properties PropTwo, PropThree and PropFour.

JonnyBee replied on Monday, September 27, 2010

Hi,

Back to your orignal question -  How to disable rule checking:

      var icr = (ICheckRules) viewModel.Model;
      icr.SuppressRuleChecking();

// set the properties of Model
// No rules will be exected

      icr.ResumeRuleChecking();
// set property here - rules wll be called

// check valid/invalid

 

rault replied on Tuesday, September 28, 2010

Outstanding, thank you!

I hope the new book comes out ahead of schedule.

Copyright (c) Marimer LLC