[rspec-users] rspec-rails render_views doesn't render layouts? / how to check flash isn't rendered

David Chelimsky dchelimsky at gmail.com
Mon Aug 29 00:23:08 EDT 2011

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
> end

I 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.


More information about the rspec-users mailing list