Skip to content

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

aCandidMind
Copy link

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 and RSpec.current_example is nil then causing the error in the title. @yujinakayama's spec repro in #1818 points to the cause being that FeatureExampleGroup doesn't include RSpec::Rails::ViewRendering while ControllerExampleGroup 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.

@aCandidMind
Copy link
Author

aCandidMind commented May 31, 2017

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.

@aCandidMind
Copy link
Author

aCandidMind commented May 31, 2017

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)
Copy link
Member

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?

Copy link
Author

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?
Copy link
Member

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)
Copy link
Member

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.

Copy link
Author

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.

Copy link
Member

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.

@b-wen
Copy link

b-wen commented Jun 2, 2017

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

@xaviershay
Copy link
Member

Fixed in #1831

@xaviershay xaviershay closed this Jun 27, 2017
@xaviershay
Copy link
Member

(sorry I didn't see this PR until after I merged the other one, but I do think it addresses the feedback in here)

@JonRowe JonRowe mentioned this pull request Jun 27, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants