Re: OO Unit Test Question
- From: "H. S. Lahman" <h.lahman@xxxxxxxxxxx>
- Date: Wed, 28 Dec 2005 16:25:35 GMT
Responding to Kk_oop...
Here's a unit test question.
- Consider class A and Class B. - A has an attribute of type B.
If A and B are both problem space objects, then the answer is: Don't Do That. A and B should be peer objects that are linked by a relationship rather than having B instantiated within A's implementation and they should collaborate normally.
- A.doA( ) calls B.doB( int x). - B.doB returns a bool needed by A. It is false if parameter x is not valid.
I agree with Campbell. You need to define the contract. If passing a bad 'x' is an error, then it should be handled by an assertion and exception handling rather than a return value that A uses.
If part of B's contract is to evaluate 'x' based on some context rules, then B should communicate failure to A in a separate message, as in:
A::doA()
...
myB->doB(x) // no return value dependencyA::respondToBadX() // do whatever A would in response to the FALSE return in your code
B::doB(x)
...
if (!<some test of B's context)>)
myA->respondToBadX()The problem here is that if A::doA uses the return value of B::doB, then there is no way to unit test A::doA without a valid implementation of B::doB. That's because the A::doA method has an implementation dependency on the specification on what B::doB does, namely that B::doB correctly determines whether 'x' is valid.
[Stubbing B::doB in the test harness to return an appropriate value for the test case is just a form of self-delusion; one is just testing the test harness, not the contract between A and its client. The specification of A::doA from its client's perspective necessarily includes the correct specification of B::doB because the results of whatever A::doA does for its client depends upon 'x' being correctly evaluated.]
Note that in my solution this is not a problem. When unit testing A::doA, all one has to demonstrate is that B::doB was called with the correct 'x'. The part of the specification of A that depends on whether 'x' is valid or not has been delegated to A::respondToBadX where the validity is an input (i.e., that the method was called at all).
************* There is nothing wrong with me that could not be cured by a capful of Drano.
H. S. Lahman hsl@xxxxxxxxxxxxxxxxx Pathfinder Solutions -- Put MDA to Work http://www.pathfindermda.com blog: http://pathfinderpeople.blogs.com/hslahman (888)OOA-PATH
.
- Follow-Ups:
- Re: OO Unit Test Question
- From: Phlip
- Re: OO Unit Test Question
- References:
- OO Unit Test Question
- From: kk_oop
- OO Unit Test Question
- Prev by Date: Re: Empty class extensions?
- Next by Date: Re: Empty class extensions?
- Previous by thread: Re: OO Unit Test Question
- Next by thread: Re: OO Unit Test Question
- Index(es):
Relevant Pages
|