-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Fix undefined method `example_group' for nil & NoMethodError: render_views? for feature specs #1827
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
…views? for feature specs
Maybe the author of #1732 @andymorris can also have a look at whether I preserved all behavior. As I said tests are green and to me it looks good. But I'm not deep into RSpec behavior. PR #1732 seemed to work for controller specs but not for feature specs. This PR here is trying to fix this. |
e.g. skip it for feature specs
ad40aae is only an optimization for the case when no controller specs are run. E.g. when only running feature specs. |
end | ||
end | ||
|
||
LogSubscriber.attach_to(:action_view) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why did this have to move?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's only an optimization for the case when no controller specs are run. E.g. when only running feature specs it should make things a little faster since not every template rendering is going through that Subscriber and so on. I could easily refrain from moving it.
return if current_example_group.render_views? | ||
info(" Template rendering was prevented by rspec-rails. Use `render_views` to verify rendered view contents if necessary.") | ||
example_group = current_example_group | ||
if example_group.respond_to?(:render_views?) && !example_group.render_views? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We prefer guard condition style returns, prehaps just change the first line to:
return if !example_group.respond_to?(:render_views?) || !example_group.render_views?
RSpec.current_example.example_group | ||
# When running feature specs current_example can be nil if the subscriber runs in a different context | ||
# than the specs themselves. E.g. in a capybara thread. | ||
RSpec.current_example.try!(:example_group) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please don't use try!
here, we try to avoid the usage of non standard Ruby, you could add a guard to return nil if theres no current example but really we should figure out why its nil.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
RSpec.current_example
is nil
in the case where the request came from the (Selenium) webdriver out of the browser (e.g. via AJAX) and not from an RSpec example group. I'll add that little extra info to the code comment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No don't thats not the cause, this is only executed during a test process when this should have a thread value, its not selenium causing it.
To mark Feature spec as Controller spec by :type => :controller like following could avoid this error message. Actually it's indeed Feature spec other than Controller spec, so I think this way is improper: describe "products page", :type => :controller do |
Fixed in #1831 |
(sorry I didn't see this PR until after I merged the other one, but I do think it addresses the feedback in here) |
This is my stab at issue #1800.
In my case the
ViewRenderer
code that was added with PR #1732 was triggered via the capybara webdriver andRSpec.current_example
is nil then causing the error in the title. @yujinakayama's spec repro in #1818 points to the cause being thatFeatureExampleGroup
doesn't includeRSpec::Rails::ViewRendering
whileControllerExampleGroup
does.This commit makes the tests in PR #1818 pass (via the
respond_to?
) and running the whole rspec-rails suite only caused bundler errors for me (I tweaked the gemspec for local gem development). Using this local commit also got rid of loads of errors (those from the title) when running feature specs in my own project.I have to say that this is my very first experience with RSpec code and that I just deemed the
included
block to be a good place to do this. Bear with me, if I forgot something.