[rspec-users] shared examples sharing methods

Nicolás Sanguinetti godfoca at gmail.com
Thu Jun 18 01:04:22 EDT 2009

On Thu, Jun 18, 2009 at 1:36 AM, Stephen Eley<sfeley at gmail.com> wrote:
> On Thu, Jun 18, 2009 at 12:17 AM, Ben Mabey<ben at benmabey.com> wrote:
>> If you do go down that route, I recommend method calls instead of instance
>> variables.  That way it will yell out you when you forget to define one. :)
> Hey, instance variables do that too.  Yelling == "my tests fail."  >8->
> (And if they don't, I'm *really* doing something wrong...)

But methods are more explicit. Suppose you want to parametrize on the
value of a certain property named "bar". Not defining  @bar will raise
an NoMethodError because you called something on nil. Not defining the
method will raise a NoMethodError because bar isn't defined, which
tells you immediately what you need to solve.

You could also do something like

def bar
  raise NotImplementedError, "please define this method in each of
your example groups that use this shared behavior"

and it will be even more explicit. *But* this means you have to define
the method *after* you include the shared examples, or it will fail
(which is a code smell, IMO).


> --
> Have Fun,
>   Steve Eley (sfeley at gmail.com)
>   ESCAPE POD - The Science Fiction Podcast Magazine
>   http://www.escapepod.org
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users

More information about the rspec-users mailing list