0

こんにちは私は現在、テストに関するチュートリアルを行っています。ナビゲーションバーのタブを強調表示するためのヘルパーメソッドを構築しています。ヘルパーメソッドは次のとおりです。

module RecipesHelper
  def tabs(current_tab)
    content_tag(:div, links(current_tab), :id => "tabs")
  end

  def links(current_tab)
    nav_items.map do |tab_name, path|
      args = tab_name, path

      if tab_name == current_tab
         args << {:class => "current"}
      end
      link_to *args
    end.join(separator).html_safe
  end

  def nav_items
    {
      "New" => new_recipe_path,
      "List" => recipes_path,
      "Home" => root_path
    }
  end

  def separator
    content_tag(:span, "|", :class => "separator").html_safe
  end
end

これがテストの1つです:

require 'test_helper'
class RecipesHelperTest < ActionView::TestCase
  test "current tab is correct" do
    render :text => tabs("New")
    assert_select "a[class='current']" do |anchors|
      anchors.each do |anchor|
        assert_equal new_recipe_path, anchor.attributes['href']
      end
    end
  end
end

私の質問はanchors、ブロックで渡される変数は何ですか?ネストされたブロックについても同じ質問ですanchor。ありがとう。

4

1 に答える 1

2

assert_selectのドキュメントを読むと、一致するすべての要素のリストが表示されます。この場合a[class='current']、おそらくタグ(アンカー)のリストが返されます。

次に、そのリストをループし、hrefその単一のタグの属性がnew_recipe_path

assert_select docs

于 2012-12-01T21:02:48.297 に答える