[rspec-users] named route failing in class spec

David Chelimsky dchelimsky at gmail.com
Sun May 2 14:23:14 EDT 2010

On Sun, May 2, 2010 at 1:48 AM, Patrick J. Collins
<patrick at collinatorstudios.com> wrote:
> I have something like:
> class Foo
>  def initialize(template)
>    @template = template
>  end
>  def link
>    link_to "foo", foo_path
>  end
>  def method_missing(*args, &block)
>    @template.send(*args, &block)
>  end
> end
> ...
> before(:all) do
>  @foo = Foo.new(self)
> end

Generally speaking, before(:each) is a better choice than
before(:all). Each example gets run in its own instance, and using
before(:all) risks leaking state from example to example.

The pattern that you're implementing here is called the Self-Shunt
Pattern, in which the example itself acts like a player in the code
being tested. This is usually done to instrument the code so the
example can observe calls made to other objects (like a mock). This is
a pattern that I believe pre-dates mock objects, and is rarely
implemented these days because mocks allow us to better separate

Of course, this is a bit different from what's going on here. Here the
example is being used as a delegate to handle services not available
to the object. Named routes are made available to controller, view,
and helper specs, so you can get them by either storing this file in
any of those folders (spec/helpers, for example).


> it "should have a link" do
>  @foo.link.should match /href/
> end
> ...  But I get this error:
> NoMethodError in 'FooRenderer should have a link'
> undefined method `foo_path' for
> #<ActiveSupport::TestCase::Subclass_1:0x105619710>
> ...  What can I include in my spec to get foo_path to be recognized?

More information about the rspec-users mailing list