evilguc
evilguc

Reputation: 930

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.

Upvotes: 2

Views: 943

Answers (2)

Michael F
Michael F

Reputation: 1409

In example groups, write Rails.application.routes.url_helpers.root_path instead of just root_path. You could create your own helper method to make repeated calls short.

Upvotes: 1

Josh Rieken
Josh Rieken

Reputation: 2266

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.

Upvotes: 2

Related Questions