I’m trying to DRY up my RSpec examples by adding a few controller macros for frequently used tests. In this somewhat simplified example, I created a macro that simply tests whether getting the page results in a direct to another page:
def it_should_redirect(method, path)
it "#{method} should redirect to #{path}" do
get method
response.should redirect_to(path)
end
end
I’m trying to call it like so:
context "new user" do
it_should_redirect 'cancel', account_path
end
When I run the test I get an error saying that it doesn’t recognize account_path:
undefined local variable or method `account_path’ for … (NameError)
I tried including Rails.application.routes.url_helpers per the guidance given in this SO thread on named routes in RSpec but still receive the same error.
How can I pass a named route as a parameter to a controller macro?
The url helpers included with
config.include Rails.application.routes.url_helpersare valid only within examples (blocks set withitorspecify). Within example group (context or describe) you cannot use it. Try to use symbols andsendinstead, something likeDon’t forget to include url_helpers to config.
Or call the macro inside example: