Skip to content

Log a message when view rendering is intercepted #1732

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

Merged
merged 1 commit into from
Oct 31, 2016

Conversation

andymorris
Copy link
Contributor

This is an attempt at #1332, to clarify test logs when view rendering is prevented. Without calling render_views, logs currently look something like:

I, [2016-10-29T00:26:15.783195 #47754]  INFO -- : Processing by FoosController#show as HTML
I, [2016-10-29T00:26:15.803927 #47754]  INFO -- :   Rendered foos/show.html.haml within layouts/bar (6.3ms)

...even though the template was not actually rendered. With this change the logs look like:

I, [2016-10-29T00:27:02.543168 #47770]  INFO -- : Processing by FoosController#show as HTML
I, [2016-10-29T00:27:02.551786 #47770]  INFO -- :   Rendered foos/show.html.haml within layouts/bar (0.2ms)
I, [2016-10-29T00:27:02.551891 #47770]  INFO -- :   Template rendering was prevented by rspec-rails. Use `render_views` to verify rendered view contents if necessary.

I struggled a little bit with how to write the specs for this. The LogSubscriber is attached and created independent of any examples/groups, but still needs to know the current example group's render_views? to decide whether or not to log the message, though when rspec-rails specs are running the current example is in the rspec-rails suite and not a controller spec. I'd be happy to take a different approach if someone points me in the right direction.


def render_template(event)
unless current_example_group.render_views?
info " Template rendering was prevented by rspec-rails. Use `render_views` to verify rendered view contents if necessary."
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you add parentheses here?

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.

Also could you add parentheses here?

end

def render_template(event)
unless current_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.

Our rubocop configuration would like you to change this to

return if current_example.render_views? and then flatten the conditional. Could you do that?

RSpec.current_example.example_group
end

def render_template(event)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you change this to _event to pass the rubocop review?

@fables-tales
Copy link
Member

@andymorris thanks for submitting this! Looks like a good solution to the problem. I've left you some review comments, if you address those, I'd love to merge this in ✨

@andymorris
Copy link
Contributor Author

Changes made, and rubocop no longer shows any issues. Let me know if there is anything else.

@fables-tales
Copy link
Member

LGTM

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.

2 participants