Rspec-rails: Helper specs should work with engine routes.

Created on 16 Dec 2014  路  10Comments  路  Source: rspec/rspec-rails

currently I'm getting an error:

undefined method `folders_path' for #<#<Class:0x007fbaa7f1a400>:0x007fbaa894d4b8>

I'd like to be able to add routes like we do in Controller specs:

routes { MyEngine::Engine.routes }

Currently that doesn't work in helper specs:

undefined method `routes' for RSpec::ExampleGroups::MyEngine::FooHelper::Class
Engine

Most helpful comment

Ended up shoving this in spec/support/url_helpers.rb:

RSpec.configure do |config|
  config.before :each, type: :helper do
    helper.class.include MyEngine::Engine.routes.url_helpers
  end
end

Maybe this should be in rspec-rails?

All 10 comments

It does work if I add the engine context to the helper itself:

polymorphic_url([my_engine, @folder])

But the app itself seems to work fine without the context, it's just needed so that the RSpec test will pass.

Ended up shoving this in spec/support/url_helpers.rb:

RSpec.configure do |config|
  config.before :each, type: :helper do
    helper.class.include MyEngine::Engine.routes.url_helpers
  end
end

Maybe this should be in rspec-rails?

There's no way for us to know the engine you're testing so we'd have to go down the route suggested by @jcoyne instead, I don't know if @cupakromer has the bandwidth to work on this right now so PR's welcome...

I am looking into some engine stuff at work. I'm planning on starting the process to address all of the engine related issues later this week.

Not sure if this is relevant or not, but I was getting the same error because I wasn't passing type: :controller to the initial describe line:

describe MyEngine::PostsController, type: :controller do
  routes { MyEngine::Engine.routes }
end

As a followup to @botandrose solution, this one doesn't leak between the spec examples:

RSpec.configure do |config|
  config.before :each, type: :helper do
    helper.extend MyEngine::Engine.routes.url_helpers
  end
end

if the above solutions doesn't work, try loading helper urls as rspec configs as below:

RSpec.configure do |config|
  config.include MyEngine::Engine.routes.url_helpers
end

This is a legit bug, but work on engines is generally de-prioritised by the rest of the core team. If someone wants to open a PR on this, I will gladly review and accept it. I'd say that this won't receive action until at least the RSpec Rails 4 branch is done, but given the age is unlikely to receive a lot of attention.

The difficulty with adding it at the rails_helper.rb level is it replaces all url_helpers for the entire suite including if scoped to a specific type: (just replaces for all that type).

If you want it for a specific spec, run it in a before on that particular group:

# spec/feature/my_included_engine_feature_spec.rb`
before { config.include MyEngine::Engine.routes.url_helpers }

This will replace all core routes with the engine routes. I didn't need to "add" it to the core routes, so that is not solved here.

We acknowledge this is an area that needs improvement, we just don't have time to help, if you do please contribute a PR and we'll happily help review it and get it in.

Was this page helpful?
0 / 5 - 0 ratings