rspec-rails: View helpers not available in view specs

Just noticed that view helpers aren’t automatically included in views like they were in RSpec 1, so you need to include them explicitly in all your view specs:

before do
  view.extend ApplicationHelper
  view.extend FooHelper
end

Is this intentional or still on the “to do” list?

Cheers, Wincent

About this issue

  • Original URL
  • State: closed
  • Created 14 years ago
  • Comments: 30 (22 by maintainers)

Commits related to this issue

Most upvoted comments

OK - looks like this is an integration problem. RSpec is asking the controller for it’s helpers and adding them to the view: https://github.com/rspec/rspec-rails/blob/master/lib/rspec/rails/example/view_example_group.rb#L127-130

but the controller is a test controller, not any controller in your app: https://github.com/rails/rails/blob/master/actionpack/lib/action_view/test_case.rb#L100

We need a way to declare the controllers we want to grab helper methods from. In the short term you can do something like this (I think - untested - try it):

def add_controller_helpers *controllers
  controllers.each { |c| view.singleton_class.class_eval { include c.new._helpers } }
end

before do
  add_controller_helpers ApplicationController, FoosController, BarsController
end

I’d consider adding add_controller_helpers (or similar) to rspec-rails, but I think this really belongs in Rails proper. Either way, there is no reliable way for RSpec or Rails to infer which controller to use all of the time, so it’s probably best to leave that to the user.

Thoughts?