开发者

rspec render_views ignores layouts? want to test static cached page does not display flash message

开发者 https://www.devze.com 2023-03-31 13:13 出处:网络
I开发者_如何学Python\'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 (Devi

I开发者_如何学Python'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
      # edit: the following flash lines don't do anything
      # it's not the right flash object, this one is intended for
      # inspecting after request not setting before request
      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)

Thanks, Nick


Turns out this isn't the right approach. It should be an integration test (cucumber, request spec, etc) as it's testing several layers. That and rails doesn't seem to render the templates inside their layouts at this level.

So in integration test: Set up a flash message by making a request to a controller which does nothing else (create a dummy controller & routing in your test code), then hit the page of concern and make sure the flash notice isn't rendered.

e.g. https://gist.github.com/1178383

It seems like a long way round but it will cover it.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号