Re: OO Unit Test Question



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 dependency

A::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



.



Relevant Pages

  • Re: When static typing is worth it
    ... I wrote 50kLOC of custom object marshalling code in OCaml and the ... client insisted on unit testing it. ... caught many bugs and 24hrs of running unit tests caught precisely zero ... It gives you (or your client) a warm and fuzzy feeling when the ...
    (comp.lang.lisp)
  • Re: Why does python not have a mechanism for data hiding?
    ... at how unit testing is done is C++, Java, and Ada. ... Don't test private functions. ... or the client is not smart enough to understand what they need. ... the solution is smarter programmers and clients rather than a dumber ...
    (comp.lang.python)
  • Re: Consuming Webservice
    ... client page and everything seems to be working OK. ... SOAP message directly to test my webservice(without the use of proxy ... You can write a test harness which reads the XML file, ...
    (microsoft.public.dotnet.framework.webservices)
  • Re: Binary message creation in BizTalk Server
    ... In my CreateMessage method, I need to pass a XLangMessage variable. ... How will I pass this and do a unit testing with a client application? ... I could not find much info in MSDN also ...
    (microsoft.public.biztalk.general)
  • Re: Abstract Factory of Factory??
    ... This is up to the news client. ... clients should always use a fixed font for such messages. ... the reading client needs to be set up properly as well. ... Pathfinder Solutions -- Put MDA to Work ...
    (comp.object)