strasbal
strasbal

Reputation: 141

Rails Integration Test Syntax

In Michael Hartl's Ruby on Rails Tutorial, the author implements integration tests to test layout links. He uses the syntax:

test "layout links" do

Is the role of "layout links" to inform the reader what the test is doing, or is it part of the code?

Further down, he runs an assert_select to test if the href links are working:

assert_select "a[href=?]", help_path   

Why is [href=?] in brackets and what role does the question mark play?

Full code for test/integration/site_layout_test.rb below:

require 'test_helper'
class SiteLayoutTest < ActionDispatch::IntegrationTest
  test "layout links" do
    get root_path
    assert_template 'static_pages/home'
    assert_select "a[href=?]", root_path, count: 2
    assert_select "a[href=?]", help_path
    assert_select "a[href=?]", about_path
    assert_select "a[href=?]", contact_path
  end
end

Upvotes: 0

Views: 39

Answers (1)

Thomas Walpole
Thomas Walpole

Reputation: 49890

"layout links" is just used to name the test

assert_select is documented here and can take a CSS selector expression with substitution values ( the ? is where the substitution takes place ) so

assert_select "a[href=?]", help_path

is basically the same as

assert_select "a[href=#{help_path}]"

Upvotes: 1

Related Questions