Unit Test for Broken Rules

Unit Test for Broken Rules

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


rxelizondo posted on Thursday, April 28, 2011

Hello,

When my new object is created (IsNew == true), the object starts up with several broken rules. So I thought I would be nice to create a unit test that creates a new object and makes sure that the appropriate initial broken rules are present.

This is all good but my problem is that I am not sure how to accomplish this in a strongly typed manner since there is not “compiler connection” between the BrokenRule and a BusinessRule classes.

In an ideal word, I would like the BrokenRule class to have two extra properties:

 

1) BusinessRule: This value is basically the type of the BusinessRule that is the originator of the broken rule (A class that inherits from BusinessRule).

2) Property: This value is the PropertyInfo associate to the broken rule. This is basically the same as the BrokenRule.Property value but instead of it being a string, I would like it tot be of type of PropertyInfo.

 

With these two new properties on the BrokenRule class, I can now test to make sure that the BrokenRule is the correct BrokenRule I am looking for, and do it on a strongly typed way like:

If(myBrokenRule.BusinessRule = typeof(MaxLength) & myBrokenRule.Property == SomeClass.ComeProperty)

{

 // This rules is the rule I am looking for, and I

// found this in a strongly typed way.

}

 

Am I just approaching this all wrong and asking a dumb question as usual? Is anyone else out doing unit test on broken rules? And if so, how are you handling this?

Thank you.

 

rxelizondo replied on Thursday, April 28, 2011

 

mmm… never mind. I think I found a way:

 

var x = new Csla.Rules.CommonRules.Required(SomeClass.SomeProperty);

var y = someObject.BrokenRulesCollection[0];

Assert.IsTrue(x.RuleName == y.RuleName);


This will require to have the BusinessRule set as public but I guess that not big deal. I also realize that the BrokenRulesCollection is not guarantied to be on a given order, the code above that uses the indexer [0] to get the first rule expecting it represents the rule I am looking for is just an sample code.

Thanks.

 

RockfordLhotka replied on Thursday, April 28, 2011

I frequently use a LINQ query on the broken rules collection to pull out the specific item(s) I'm looking for.

ajj3085 replied on Saturday, April 30, 2011

I peronally query the BRC based on property name, then dig deeper to see if the rulename matches something I expect.

Copyright (c) Marimer LLC