Search code examples
ruby-on-railsrspec2helper

named route as a parameter for example group in rspec


I am newbie in testing and using RSpec and need some help:

I have shared example group:

shared_examples_for 'request which do something' do |opts = {}|
  respond.should redirect_to(opts[:redirect_to])
end

In my spec file:

describe "behavior" do
  it_should_behave_like 'request which do something', :redirect_to => root_path
end

Looks great, but I get this error:

Exception encountered: #<NameError: undefined local variable or method `root_path' for #<Class:0x000000069e2838>>

and it points on line with 'it_should_behave_like ... '

I tried to include Rails.application.routes.url_helper in spec_helper, but it doesn't work anyway.

By the way it perfectly works from example like this:

describe "behavior" do
  it "should redirect" do
    response.should redirect_to(root_path)
  end
end

(even without explicit including url_helpers)

Thanks for any help.


Solution

  • You can't use path helpers within example groups, but there is a workaround. See this answer:

    Passing a named route to a controller macro in RSpec

    With this, you can pass a symbol and use send.