Apologies for the generic title, but I couldn't really sum this up in a few words.
When writing a Unit Test, the goal is obviously to test the public interface of a class, to ensure it behaves as expected. I have at least one test (method) for each method of a class, but I find when testing a particular method, I usually have to rely on another method of the same class to verify the correct behaviour of the method I'm testing. I generally try to avoid doing this, but often I don't have a choice as many of the classes properties are protected.
What's the general stance on this? Should you avoid using other methods of the same class to verify the correct behaviour of the method your testing, or is it really not a problem?
Here's an example of what I mean just in case the wording is a little confusing...
As you can see in the example above, although I'm trying to test the register() method, I'm really testing both it, and the isRegistered() method. So a failure (or even a pass) of this test, really doesn't indicate what has exactly failed or succeeded.PHP Code:function testRegisterMethod ()
{
$obj = new Example;
$obj->register('test', 'example data');
$this->assertTrue( $obj->isRegistered('test') );
}






Bookmarks