【问题标题】:How to test if correct layout was used in controller using RSpec and Rails 3如何使用 RSpec 和 Rails 3 测试控制器中是否使用了正确的布局
【发布时间】:2011-04-27 09:25:07
【问题描述】:

我想编写一个 rspec 测试来测试控制器是否使用了正确的布局。 (其实我想测试一下没有使用布局:))。

我做了一些谷歌搜索,也看过这里Testing rendering of a given layout with RSpec & Rails

但所有这些都不适用于 Rails3。

我用过:

controller.layout

controller.class.read_inheritable_attribute(:layout)

但这些都没有给我实际使用的布局。

您知道如何获取控制器使用的布局吗?

【问题讨论】:

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


    【解决方案1】:

    试试response.layout

    编辑

    果然,response.layout 不再适用于 Rspec2。但是,您可以验证使用render_template 呈现的正确布局,如Rails Forum thread 所述:

    response.should render_template("layouts/mylayout")
    

    至于您问题的第二部分,我看不到一种检查布局是否存在 absence 的方法。 response.should_not render_template("layouts/mylayout") 似乎不起作用。见this discussion

    【讨论】:

    • 不,对于 RoR3 中的响应对象,没有像 layout 这样的方法:NoMethodError(#<:response:0x9322ea0> 的未定义方法“布局”):
    猜你喜欢
    • 1970-01-01
    • 2011-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多