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

标签 ruby-on-rails ruby rspec haml

在 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/

相关文章:

ruby-on-rails - Rails:类别和子类别模型Rails

ruby - 如何紧凑地编写一个执行带有参数的方法的 block

javascript - 是否有用于在 JavaScript 中生成概率分布的科学库?

ruby-on-rails - 在项目管理器应用程序中测试创建新项目的最佳方法

ruby-on-rails - rails : Show action in controller is unable to access foreign key value

javascript - 在 Rails 应用程序和 Node.js 应用程序之间进行通信

ruby-on-rails - 设置 Rails 中搜索字段的默认值

mysql - Rails Mysql ActiveRecord::ConnectionNotEstablished

ruby - 如何在 Ruby 字符串中插入项目符号字符?

ruby - ruby 中的数组推送弹出