【问题标题】:rspec render_views ignores layouts? want to test static cached page does not display flash messagerspec render_views 忽略布局?想测试静态缓存页面不显示flash消息
【发布时间】:2011-08-28 13:55:50
【问题描述】:

我正在尝试测试通过 rails 生成的“静态”页面(它们是 ERB,但被缓存),不会呈现身份验证系统 (Devise) 或其他任何地方留下的任何杂散闪存通知。

我已尝试编写此控制器规范,但似乎 response.body 仅呈现模板,而不是其布局?

  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

我已尝试更改为渲染 Flash 通知的布局,甚至将文本插入到布局模板中,但无法使规范失败。

有没有办法让 rspec 也用适当的布局来渲染模板?

控制器规范是否是尝试执行此操作的错误方法? 这似乎不合适,因为它更多地与正在使用的布局及其内容有关,但渲染过程从控制器开始,它接收结果,我可以在渲染之前操纵闪存哈希内容。

版本: 导轨(3.0.10), rspec-rails (2.6.1), rspec-core (2.6.4)

谢谢,尼克

【问题讨论】:

    标签: ruby-on-rails-3 rspec rspec-rails


    【解决方案1】:

    事实证明这不是正确的方法。它应该是一个集成测试(黄瓜、请求规范等),因为它正在测试多个层。这和 rails 似乎没有在这个级别的布局中呈现模板。

    所以在集成测试中: 通过向不执行任何其他操作的控制器发出请求(在测试代码中创建虚拟控制器和路由)来设置 Flash 消息,然后点击关注的页面并确保未呈现 Flash 通知。

    例如https://gist.github.com/1178383

    这似乎是一个很长的路要走,但它会覆盖它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-05
      • 1970-01-01
      相关资源
      最近更新 更多