Business Rule Testability

Business Rule Testability

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


dpk posted on Friday, June 18, 2010

I'd like to be able to write unit tests for the custom business rules I create so that I can verify the rules work as expected without having to test them through a business object that uses them. RuleContext does not have a public constructor and there is no interface that can be implemented in a test harness class to provide the context for the Execute method. Ideas?

RockfordLhotka replied on Saturday, June 19, 2010

Yeah, that's a good point, and I encountered the same thing recently.

Many tests interact with the Target property - in which case you can't really unit test the rule in isolation anyway, so this is a non-issue.

But for tests that exclusively use the input/output property dictionaries it would be possible to fabricate a context against which the rule could run. I am considering adding a public ctor that loads the input dictionary, which would address this unit test requirement.

dpk replied on Saturday, June 19, 2010

I agree testing in isolation is a non-issue. I was simply stating that I do not want to test the business rules through a business object. What I'd like to see is a way to inject a RuleContext into the Execute method. This could be accomplished by providing a public IRuleContext interface from which I can build a mock / stub / double to inject into the Execute method. The mock RuleContext would allow me to provide a mock business object to the Target property or set input / output dictionaries as required by the rule. The key here is that I'm injecting my test context rather than having it injected by the CSLA business rules framework.

I'm only just getting familiar with the new business rule framework so I'm not sure that such an interface would work in all circumstances.

Dave

Copyright (c) Marimer LLC