nruth
2011-Aug-28 13:57 UTC
[rspec-users] rspec-rails render_views doesn''t render layouts? / how to check flash isn''t rendered
I''m trying to test that "static" pages (they''re ERB, but get cached), generated through rails, don''t render any stray flash notices left over by the authentication system (Devise) or wherever else. I''ve tried writing this controller spec, but it appears that response.body only renders the template, not its layouts? describe "so that static caching can be used" do render_views specify "flash notices are not rendered" do flash[:notice] = "flash boo" flash[:error] = "flash boo" flash[:alert] = "flash boo" get :show, :page => ''privacy_policy'' response.body.should have_content(''flash boo'') end end class StaticPagesController < ApplicationController layout ''master'' def show response.headers[''Cache-Control''] = "public, max-age=#{6.hours}" render "static_pages/#{params[:page]}" end end I''ve tried changing to a layout which does render flash notices, and even inserting the text into the layout template, but can''t make the spec fail. Is there a way to ask rspec to render the template with the appropriate layouts as well? Is a controller spec the wrong way to try and do this? It seems out of place, as it''s more to do with which layouts are being used, and their contents, but the rendering process starts at the controller, it receives the result, and I can manipulate the flash hash contents before rendering. Versions: rails (3.0.10) rspec-rails (2.6.1) rspec-core (2.6.4) I''ve also posted on stackoverflow @ http://stackoverflow.com/questions/7221388/rspec-render-views-ignores-layouts-want-to-test-static-cached-page-does-not-disp Thanks, Nick -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20110828/cf42b7d8/attachment.html>
David Chelimsky
2011-Aug-29 04:23 UTC
[rspec-users] rspec-rails render_views doesn''t render layouts? / how to check flash isn''t rendered
On Aug 28, 2011, at 8:57 AM, nruth wrote:> I''m trying to test that "static" pages (they''re ERB, but get cached), generated through rails, don''t render any stray flash notices left over by the authentication system (Devise) or wherever else. > > I''ve tried writing this controller spec, but it appears that response.body only renders the template, not its layouts?If you use render_views, as in the example below, layouts should be rendered. This is managed by Rails, not RSpec (without render_views, rspec-rails stubs the view, but with render_views you''re getting exactly what you''d get in a Rails functional test).> describe "so that static caching can be used" do > render_views > specify "flash notices are not rendered" do > flash[:notice] = "flash boo" > flash[:error] = "flash boo" > flash[:alert] = "flash boo"These lines ^^ have no effect. The flash object before the get (below) is not the same as the flash object used in the controller.> get :show, :page => ''privacy_policy'' > response.body.should have_content(''flash boo'') > end > end > > class StaticPagesController < ApplicationController > layout ''master'' > > def show > response.headers[''Cache-Control''] = "public, max-age=#{6.hours}" > render "static_pages/#{params[:page]}" > end > endI guess I''m confused about your goal here: are you trying to specify that the page, when it gets cached, doesn''t include any flash messages? If that''s the case, then you''d probably want to do this in an integration test where you hit the url that causes the page to be cached, followed by a request for the cached page itself. HTH, David
nruth
2011-Aug-29 11:38 UTC
[rspec-users] rspec-rails render_views doesn''t render layouts? / how to check flash isn''t rendered
Hi David Something like: Given I am a user about to receive an arbitrary flash notice When I visit one of the application''s static pages which has not yet been cached Then I should not see my flash notice in the page And another user should not see my flash notice when they visit the same page afterwards The aim is to produce a sort of tripwire test which checks that nobody has added <%= flash[:notice] %> or similar to any of the ERB files involved in rendering this page. It''s to prevent a problem where someone might log out (devise), be sent to / and trigger static (or varnish) page caching saying "You have been logged out" which everyone else would then also see. I agree it''s more of an integration test, as it''s testing the view and controller, so I''ll try a request spec with dummy controller/routing to set a flash message next. I''d thought I could manipulate the flash in a controller spec, but now I remember it''s for inspecting it after a request. Thanks for the help Nick -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20110829/06c9da49/attachment-0001.html>
nruth
2011-Aug-29 13:35 UTC
[rspec-users] rspec-rails render_views doesn''t render layouts? / how to check flash isn''t rendered
I''ve shared my solution here in case it''s of interest: https://gist.github.com/1178383 I don''t like the use of reload_routes! but it seems to do the trick. Regards, Nick -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20110829/a2849f4f/attachment.html>