【问题标题】:RSpec2 testing HAML helperRSpec2 测试 HAML 助手
【发布时间】:2012-07-15 21:02:18
【问题描述】:

我正在使用 HAML 助手来绘制菜单

module ApplicationHelper
  def draw_menu
    items = %w[
      item1
      item2
    ] # community

    capture_haml do
      haml_tag :nav, :class => "main-menu" do
        haml_tag :ul do
          items.each do |item|
            css_class = nil
            css_class = "first-item" unless item != items.first
            css_class = "last-item" unless item != items.last

            haml_tag :li, :class => css_class do
              haml_concat link_to t("nav.#{item}")
            end
          end
        end
      end

      haml_tag :div, :class => "clearize"
    end
  end
end

我正在尝试测试它(我想匹配预期的输出为/<nav class='main-menu'>\s*<\/nav><div class='clearize'><\/div>/

我实际做的是

require "spec_helper"

describe ApplicationHelper do

  before(:all) do
    init_haml_helpers
  end

  it "should draw a menu" do
    draw_menu().should_not be_empty
  end

end

但它不断收到以下错误:

1) ApplicationHelper should draw a menu
   Failure/Error: draw_menu().should_not be_empty
   ActionController::RoutingError:
     No route matches {}
   # ./app/helpers/application_helper.rb:20:in `block (5 levels) in draw_menu'
   # ./app/helpers/application_helper.rb:19:in `block (4 levels) in draw_menu'
   # ./app/helpers/application_helper.rb:14:in `each'
   # ./app/helpers/application_helper.rb:14:in `block (3 levels) in draw_menu'
   # ./app/helpers/application_helper.rb:13:in `block (2 levels) in draw_menu'
   # ./app/helpers/application_helper.rb:12:in `block in draw_menu'
   # ./app/helpers/application_helper.rb:11:in `draw_menu'
   # ./spec/helpers/application_helper_spec.rb:10:in `block (2 levels) in <top (required)>'

为什么? 我怎样才能让它按预期工作?

谢谢!

【问题讨论】:

  • 你用一个字符串作为参数调用link_to,这看起来很可疑。
  • 是的,你没看错!顺便说一句,这是在普通模板中工作的:-S 我将来会处理它。您能否将其发布为答案,以便我接受?谢谢。

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


【解决方案1】:

如果您调用link_to,只传递一个字符串作为该参数,那么该字符串将用作链接文本。链接的目标仅由当前操作和控制器给出:url_for(底层帮助器)只是将您提供的参数与构成当前操作的参数合并。这就是为什么

link_to 'something', :action => :an_action

生成指向呈现视图的控制器的an_action 方法的链接。

在帮助规范中这个环境不存在:没有当前的动作或控制器,所以这不起作用

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-26
    • 1970-01-01
    • 1970-01-01
    • 2014-11-03
    相关资源
    最近更新 更多