Page 1 of 1
New to testing - testing an abstract class
Posted: Sun Aug 20, 2006 2:34 pm
by Luke
How would I test an abstract class with simpletest? I'm having a hard time with this

Posted: Sun Aug 20, 2006 2:43 pm
by feyd
You test a derived class, maybe a Mock version of it. Abstract classes cannot be instantiated on their own, so there's no direct testing.
Posted: Sun Aug 20, 2006 3:28 pm
by Chris Corbyn
What exactly do you want to test? Perhaps you could post an example and we can work through it together since I'm also picking this stuff up. Using it right now to develop a lexical analyzer

Posted: Mon Aug 21, 2006 2:47 pm
by Luke
d11... see this thread:
viewtopic.php?t=54059
I am trying to test that set of classes.... the registry is abstract and needs to be tested.
Posted: Mon Aug 21, 2006 3:29 pm
by Chris Corbyn
OK, so we've established we can't instantiate the abstract class, we have to extend it. Now, you're extending it with the session class but we don't want that clutter in our tests right? That suggests we need to create stub to use in test. In this case it's simple enough to create a stub since there are no abstract methods to implement (which begs the question, why have you made it an abstract class?)
How about this:
Code: Select all
//Purely so we can instantiate the abstract class
class RegistryStub extends Registry {}
//Just something to add to the registry
class DummyClass {}
class TestOfRegistry extends UnitTestCase
{
public function testAddedObjectIsReferenced()
{
$registry = new RegistryStub();
$dummy = new DummyClass();
$registry->register('dummy', $dummy);
$this->assertReference($dummy, $registry->get('dummy'));
}
public function testResponseIfNotRegistered()
{
$registry = new RegistryStub();
$registry->register('dummy', new DummyClass());
$this->assertNotNull($registry->get('dummy'));
$registry->unregister('dummy');
$this->assertNull($registry->get('dummy'));
}
}
Posted: Mon Aug 21, 2006 3:34 pm
by Luke
d11wtq wrote:why have you made it an abstract class?
Well it started off having abstract methods... and I didn't even realize I had weeded all of them out, but I guess it's still abstract because it's always extended (in all of my uses for it so far).
Posted: Tue Aug 22, 2006 1:50 pm
by Ambush Commander
I would make it concrete. In case someone wants to use a registry but doesn't want to go through the bother of inheritance, they could do that.
In cases when you do have abstract methods, I would go with extending into a concrete class.
There's another solution (not applicable in your case, but good to keep in mind): factor away from inheritance into composition, so that the parts that need to be tested aren't in the abstract class anymore.
For instance, if I have an abstract DB class that has a concrete implementation for query binding and I want to test that, I can extend to a concrete DB class and then test, or I can factor out the query binding to a DBQueryBinder which is concrete.