Hi I’m currently going through a tutorial on testing. We’re building helper methods for highlighting the tabs in the nav bar. Here are the helper methods.
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
here is one of the tests:
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
My question is what does the anchors variable passed in the block? Same question for the nested block with anchor. Thanks.
If you read the documentation on assert_select it will get a list of all the matching elements. In this case
a[class='current']will probably return a list of a tags(anchors).It then it loops through that list and assert_equal if the
hrefattribute of that single a tag matches thenew_recipe_pathassert_select docs