在 ApplicationHelper 中我有这样的代码:
def inside_layout layout = 'application', &block
@template.instance_variable_set '@content_for_layout', capture(&block)
concat \
@template.render :file => "layouts/#{layout}", :use_full_path => true
end
它的行为是这样的:
应用程序.html.haml:
!!!
%html
%head
...
%body
= yield
common_pages.html.haml:
- inside_layout do
...
然后 common_pages 布局在应用程序布局中呈现。
我如何使用 RSpec 测试这个助手?
当我从规范文件调用 inside_layout 时:
helper.inside_layout { }
RSpec 说错误:
ActionView::MissingTemplate in 'ApplicationHelper inside_layout should render nested layout within application layout'
Missing layout layouts/application.erb in view path
但应用程序工作正常。
最佳答案
您不能直接在 rspecs 中为助手使用渲染调用,因为在该上下文中未设置 View 路径。
建议:
- 为渲染调用使用模拟。
- 更改代码,将渲染调用移到 View 模板中,然后仅从 inside_layout 方法返回渲染调用的参数。
关于ruby-on-rails - 使用 RSpec 和 Haml 测试嵌套布局助手,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2395441/