[rspec-users] Can I use mocking in this way?

David Chelimsky dchelimsky at gmail.com
Sat Apr 3 17:32:08 EDT 2010


On Sat, Apr 3, 2010 at 8:56 AM, Vojto Rinik <zero0xxx at gmail.com> wrote:
> Hello RSpec users!
> I have one abstract class and a few classes that inherit from that abstract
> one.

Ruby doesn't have abstract classes. You can have a base class that you
don't _intend_ to instantiate directly, but there's nothing stopping
you from doing so, so it's not like an abstract class in java, which
you actually can't instantiate directly.

I've seen some cases where the initialize method is made private so
you can't just call Foo.new, so it sort of feels like an abstract
class, but even in that case you can still use send() to instantiate
one in a test:

  AbstractIshClass.send(:new)

> I'd like to test if my abstract class works with RSpec, but I can't test
> directly abstract
> class (or can I?)

Yes, you can. I'd either do that, or use a shared example group and
apply it to all of the subclasses (see
http://rspec.info/documentation/).

> and I don't wanna test some particular class that
> inherits, because
> I wanna test general behaviour.
> So I was just wondering. Can I use mocking somehow to create a subclass of
> my
> base class, add some functionality in it and test methods of the base class?

Mocks are for specifying interactions between objects, which is a
different situation than you are describing.

HTH,
David


More information about the rspec-users mailing list