Unit testing business rule validation

Unit testing business rule validation

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

CBoland posted on Monday, February 01, 2010

I want to unit test business rule validation, but am having difficulty with the approach. The crux of the problem seems to be how to determine that an instance of a specific rule exists in the the BrokenRules collection. For example:

Given a Customer object with a FirstName property, test that a new instance of Customer contains a validation rule like "FirstName is required".

I've considered comparing BrokenRule.Description, but this seems too fragile (changing the verbiage breaks the test, and it's the rule that is under test, not the verbiage). I also considered parsing BrokenRule.RuleName, but this seems too intimate (the unit test doesn't know the validation callback method name that composes RuleName).

I settled on counting the number of broken rules (satisfy a known condition then test that the broken rule count decreases by 1), but I'd like to know what others in the community are doing.


am_fusion replied on Tuesday, February 02, 2010

we have taken the approach of using the rule name:

Assert.AreEqual<int>(1, uut.BrokenRulesCollection.Count((br) => { return br.RuleName.Contains("StringRequired/LastName"); }),

"LastName was set to an invalid value but the LastName StringRequired rule was not broken.");



Copyright (c) Marimer LLC