【问题标题】:How to check what template file is actually being used如何检查实际使用的模板文件
【发布时间】:2014-03-15 14:59:03
【问题描述】:

我正在使用 Rspec 2 和 Rails 3.2。我想检查我的 rspec 测试是否在某些情况下呈现了特定文件。像这样:

it "should render mobile templates" do
 get :index

 response.should render_template(‘index.mobile.erb’)
 response.should render_template(‘layout/application.mobile.erb’)
end

上面的 rspec 代码失败如下:

 Failure/Error: response.should render_template('index.mobile.erb')
   expecting <"index.mobile.erb"> but rendering with <"pages/index, layouts/application">

我在 Rspec 1.x 中读到了 was possible。但我无法使用 Rspec 2 和 rendered_template matcher 进行此检查。

附:我找到了 rails assertion assert_template 和它的 file 选项,但是这个选项被 rendered_template 忽略了。

UPD 如果我检查index.html.erb 而不是index.html.erb 会出现同样的错误

【问题讨论】:

    标签: ruby-on-rails ruby layout rspec views


    【解决方案1】:

    我无法准确验证它,但我认为文件扩展名.mobile 是问题的原因。

    Rails 会将.mobile 视为一种模板引擎,但不是模板名称的一部分。

    您可以尝试将模板重命名为index_mobile 以检查是否正确。

    我建议使用更明确的名称,例如

    pages/index_mobile.erb
    mobile/pages/index.erb # This would be better for organization.
    pages/mobile/index.erb 
    

    【讨论】:

    • 如果我检查index.html.erb会有同样的错误
    猜你喜欢
    • 2016-01-26
    • 2019-04-08
    • 2015-03-23
    • 2014-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-05
    • 2016-08-27
    相关资源
    最近更新 更多