gpt4 book ai didi

ruby-on-rails - 使用 RSpec 和 Haml 测试嵌套布局助手

转载 作者:数据小太阳 更新时间:2023-10-29 07:18:08 27 4
gpt4 key购买 nike

在 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 方法返回渲染调用的参数。

阅读更多:http://pivotallabs.com/users/john/blog/articles/854-friday-5-11-standup-view-paths-in-rspec-helper-tests-write-attribute-for-type-inference-

关于ruby-on-rails - 使用 RSpec 和 Haml 测试嵌套布局助手,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2395441/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com