[rspec-users] How to write specification for the *base* class API?

David Chelimsky dchelimsky at gmail.com
Tue Jul 15 08:23:51 EDT 2008

On Tue, Jul 15, 2008 at 4:17 AM, Piotr Wlodarek <lists at ruby-forum.com> wrote:
> For the base class, it seems reasonable to specify public API with
> RSpec.
> However, since we deal with a base class, methods are often empty (put
> there just to show the API).
> The spec cannot be run against the base class. In consequence, the spec
> must be duplicated in every derived class, which seems to be far from
> DRY.
> How to deal with it? What is THE RSpec way to specify APIs when we face
> inheritance?

This is more a theoretical TDD/BDD question than an RSpec question.

There are a few schools of thought about this, so I'd recommend
checking out the testdrivendevelopment list at yahoo as well - but
I'll give you my two cents :)

Any subclass can override any method in the base class, so it is not
sufficient to have examples of just the base class, nor is it very
helpful to have examples for empty methods just for show if the intent
is that the base class will never get initialized directly.

If you're deriving objects from the base class, write examples for the
derived classes. If all derived classes are intended to pass the same
examples with the same input/output expectations, then you can write
shared examples and run them against instances of all the derived
classes, thus satisfying your quest for DRY.


More information about the rspec-users mailing list