rspec-rails: View tests don't include any helpers
With RSpec 3.4 / Rails 4.2, I needed to mock controller-defined helpers.
Now, with RSpec master / Rails 5, it appears I need to mock all helpers, including built-ins (model_url, etc).
Is this intentional? #1076 makes it clear that leaving out Controller Helpers is intentional. Not including any helpers at all, however, might a little extreme?
FWIW, here are the changes I needed to make to my View spec:
describe 'search/print/_property.html.haml', type: :view do
it 'displays the page' do
property = build(:property)
+
+ # rails 5/rspec master doesn't include Helper classes and built-ins (Rails 4.2/rspec 3.4 did)
+ view.class_eval('include ApplicationHelper')
+ view.class_eval('include SearchHelper')
+ # even the built-in helpers!
+ allow(view).to receive(:property_path).and_return("/property/#{property.property_id}")
+
+ # rpsec intentially wants us to stub all controller-defined helpers
# https://github.com/rspec/rspec-rails/issues/1076#issuecomment-59842447
allow(view).to receive(:search_params).and_return(Search::Params.new({}))
allow(view).to receive(:client_state).and_return(Search::Params.new({}))
+
render 'search/print/property', property: property
assert_select ":match('href', ?)", "/property/#{property.property_id}"
end
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Comments: 24 (12 by maintainers)
I have a solution. First, set
require: falsein the Gemfile. Then, addrequire 'rails-controller-testing'to your spec helper file sometime beforerequire 'rspec/rails'. Both controller specs withassignsand view specs with helpers now work for me.Obviously, the optimal solution is to remove the assigns altogether, but one thing at a time.
I must be missing something here. I have the same problem. And adding require: false fixes the view spec problem - but then controller specs don’t work. The gem github page says “rspec-rails will automatically integrate with this gem once 3.5.0 and Rails 5 are released.” - so it seems like the idea is just to require it. Is there something obvious I’m missing?
Ugh, it is indeed intermittent. It just started happening again, no code changes. You’re right, putting
require: falseon rails-controller-testing fixes the issue (for non-controller tests).I tried using your branch in my gemfile but I’m getting ‘assigns has been extracted to a gem’ errors.
Is there anything else I need to do?