[rspec-users] What's the best way to test inherited behavior?

Hongli Lai hongli at plan99.net
Thu Oct 9 13:20:40 EDT 2008

I currently have a base class and 2 subclasses. I'm struggling with 
finding the best way to test them. This is the current situation.

The base class is called AbstractServer. It's not really abstract 
because it can be instantiated and used, but some important methods are 
defined as no-ops, and are meant to be overrided by child classes. The 
spec for AbstractServer looks like:

   describe AbstractServer do
     it_should_behave_like "AbstractServer-like behavior"

     ... AbstractServer tests ...

For the child classes, their specs look like this:

   describe ApplicationSpawner do
     it_should_behave_like "AbstractServer-like behavior"

     ... ApplicationSpawner-specific tests ...

   describe FrameworkSpawner do
     it_should_behave_like "AbstractServer-like behavior"

     ... FrameworkSpawner-specific tests ...

However, this looks very ugly. 'it_should_behave_like 
"AbstractServer-like behavior"' doesn't read like a normal sentence, and 
saying that AbstractServer has AbstractServer-like behavior is redundant.

What's the best way to solve this? What are good practices for testing 
inherited behavior? Should I be testing my child classes for parent 
class behavior at all? Right now I'm doing it anyway in order to detect 
bugs that I might have missed otherwise.

More information about the rspec-users mailing list