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
Rails 5 support is in a pre beta state, but cc @samphippen
this seems like it should work... I'm gonna have to investigate.
It's worth noting view tests are not really a thing anyone writes in rails without RSpec, so there's probably some fail here.
I can't reproduce this with the latest RSpec master and Rails 5.0.0.beta2 - the url helpers are both available in the template context and the spec context. Recommend closing unless some new information comes to light.
@bronson if you can give us a rails app to clone, which demonstrates the issue, we'll happily fix. In the meantime, I'm going to close.
OK. It's still happening in my app on beta2. If nobody else is seeing this, it's probably something obscure with my app.
If anyone else runs into it, I'll take the time to isolate it. Otherwise, happy to leave it closed.
FYI running into this problem as well with route/url helpers. Investigating where the problem comes from at the moment.
@pschambacher if you can give us a reproduction case I'm more than happy to debug!
@samphippen I'd need some time but basically having a view test and calling my_model_path or my_model_url seems to explode. We're knee deep in the code atm so I'll either provide a fix or a repro later :smile:
@bronson are you using the rails-controller-testing gem? Removing it fixed our problem (but now we're getting errors on assigns and render_template obviously :smile: )
I think I might have a solution with this PR.
Yes, I am using rails-controller-testing.
Want to hear something weird? When I updated to Rails beta 2 and Rspec 3.5.0.beta1, this problem went away. I deleted the lines in green above, and everything still works.
@pschambacher, are you using the latest gems?
And rails-controller-testing 0.1.0.
Yeah I am. Using rails master actually.
Just tried my app on beta2, both with and without rails-controller-testing... Everything appears to work fine, and the helpers are included again.
Kinda smells like a good ol' ActiveSupport autodependency ordering issue? Your PR sounds plausible to me but I'm afraid I'm not seeing the issue anymore.
Ugh, it is indeed intermittent. It just started happening again, no code changes. You're right, putting require: false on 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.
gem 'rails-controller-testing', github: 'pschambacher/rails-controller-testing', require: false
Is there anything else I need to do?
@bronson there's something weird here. You should use rails-controller-testing v0.1.0 with require false (your comment says that it's using my fork) and use rspec-rails from my fork and branch.
Ah, you're right. Since then, I've tweaked our few controller tests to not need rails-controller-testing anymore.
Adding require: false to rails-controller-testing fixed the issue for me too. Thanks!
gem 'rails-controller-testing', require: false
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?
I can't seem to reproduce with a fresh Rails app, but I am getting this same problem. I'm on rspec-rails 3.5.2 and have rails-controller-testing 1.0.1 installed. Setting gem "rails-controller-testing", require: false fixes my view tests but breaks my controller tests.
I'll report back here if I can get a working repro.
@nickrivadeneira did you find a solution to it. Running into the same issue
@ankitagupta12 Unfortunately not. Like bronson, I changed my tests to not need rails-controller-testing.
I have a solution. First, set require: false in the Gemfile. Then, add require 'rails-controller-testing' to your spec helper file sometime before require 'rspec/rails'. Both controller specs with assigns and view specs with helpers now work for me.
Obviously, the optimal solution is to remove the assigns altogether, but one thing at a time.
Most helpful comment
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.