2016-01-23 157 views
0

在Michael Hartl的Ruby on Rails教程中,作者實現了集成測試以測試佈局鏈接。他使用的語法:Rails集成測試語法

test "layout links" do 

是「佈局鏈接」,告知哪些測試正在做讀者的角色,或者是它的代碼的一部分嗎?

再往下,他跑了assert_select測試是否HREF鏈接工作:

assert_select "a[href=?]", help_path 

爲什麼括號[href=?]並沒有問號起到什麼作用?對於test/integration/site_layout_test.rb下面

完整代碼:

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 

回答

1

「佈局鏈接」只是用於命名測試

assert_select是記錄here並且可以採取與替代值CSS選擇表達式(是其中取代發生),從而

assert_select 「一個[HREF =?]」,help_path

是基本相同

assert_select 「一個[HREF =#{help_path}]」

+0

謝謝,這有助於! – strasbal