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

David Chelimsky dchelimsky at gmail.com
Thu Oct 9 14:11:09 EDT 2008


On Thu, Oct 9, 2008 at 12:20 PM, Hongli Lai <hongli at plan99.net> wrote:
> 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 ...
>  end
>
> For the child classes, their specs look like this:
>
>  describe ApplicationSpawner do
>    it_should_behave_like "AbstractServer-like behavior"
>
>    ... ApplicationSpawner-specific tests ...
>  end
>
>  describe FrameworkSpawner do
>    it_should_behave_like "AbstractServer-like behavior"
>
>    ... FrameworkSpawner-specific tests ...
>  end
>
> 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.

Not sure why what you're doing works at all, but try this (which is
how it should work):

describe "AbstractServer", :shared => true do
  ...
end

describe ApplicationSpawner do
  it_should_behave_like "AbstractServer"
  ..
end

etc


More information about the rspec-users mailing list