2011-08-17 17 views
23

我的頁面應該包含一個看起來像<a href="/desired_path/1">Click to go</a>的鏈接。如何使用assert_select測試給定鏈接的存在性?

你會如何測試使用assert_select?我想檢查是否存在a標記href="/desired_path/1"。你如何測試標籤的屬性?

是否有任何資源可以解釋如何使用assert_select?我閱讀了指南和API文檔,但沒有弄清楚。有沒有建議更好的方法來做到這一點?

我正在使用Rails 3並使用內置的測試框架。

謝謝。

回答

13

您可以將任何CSS選擇器傳遞給assert_select。因此,爲了測試標籤的屬性中,您可以使用[attrname=attrvalue]

assert_select("a[href=/desired_path/1]") do |elements| 
    # Here you can test that elements.count == 1 for instance, or anything else 
end 
+0

這不再Rails中的5部作品,也不是,我相信,在導軌4.請參閱[我的答案]( https://stackoverflow.com/a/48394216/187833)爲當前的語法。 – jm3

22

在assert_select,你也可以使用一個問號的屬性值,然後用一個字符串匹配跟進。

assert_select "a[href=?]", "/desired_path/1"

還有另一種方式來使用assert_select是比較友好的,特別是如果你想匹配的部分字符串或正則表達式模式:

assert_select "a", :href => /acebook\.com\/share/

+4

第二種形式似乎只適用於元素的文本而不適用於屬性。它在我嘗試時忽略了href條件。 – Shadwell

+0

是的,這個工作,但是,我沒有看到任何關於它的文檔(除了一個例子和一個傳遞提及)。 – gamov

+6

正如@Shadwell所說,** 2nd **表單不起作用(Rails 3.2.15)。不幸的是,它不會導致斷言失敗,我相信:href選項會被忽略。如果有人想自己證明這一點,那麼寫一些在href正則表達式參數中失敗的東西,並看看assert_select是如何傳遞的。 –

15

這裏是你如何可以斷言數關於使用assert_select的鏈接的事情。第二個參數可以是一個字符串或正則表達式來測試對href屬性:

# URL string (2nd arg) is compared to the href attribute thanks to the '?' in 
    # CSS selector string. Also asserts that there is only one link matching 
    # the arguments (:count option) and that the link has the text 
    # 'Your Dashboard' (:text option) 
    assert_select '.menu a[href=?]', 'http://test.host/dashboard', 
     { :count => 1, :text => 'Your Dashboard' } 

    # Regular expression (2nd arg) tests the href attribute thanks to the '?' in 
    # the CSS selector string. 
    assert_select '.menu a[href=?]', /\Ahttp:\/\/test.host\/dashboard\z/, 
     { :count => 1, :text => 'Your Dashboard' } 

對於其他方式,你可以使用assert_select,這裏有從Rails的ActionPack的所採取的例子3.2.15文檔(見文件actionpack-3.2.15/lib/action_dispatch/testing/assertions/selector.rb ):

# At least one form element 
    assert_select "form" 

    # Form element includes four input fields 
    assert_select "form input", 4 

    # Page title is "Welcome" 
    assert_select "title", "Welcome" 

    # Page title is "Welcome" and there is only one title element 
    assert_select "title", {:count => 1, :text => "Welcome"}, 
     "Wrong title or more than one title element" 

    # Page contains no forms 
    assert_select "form", false, "This page must contain no forms" 

    # Test the content and style 
    assert_select "body div.header ul.menu" 

    # Use substitution values 
    assert_select "ol>li#?", /item-\d+/ 

    # All input fields in the form have a name 
    assert_select "form input" do 
    assert_select "[name=?]", /.+/ # Not empty 
    end 
+1

你將如何測試多個屬性?像'a [href =?數據富= 「酒吧」]]'? – Mohamad

+1

@Mohamad,我認爲這樣做,每個屬性都在它自己的方括號中: 'assert_select'a [href =?] [data-foo =「bar」]','http:// test。主機/儀表板' –

2

對於任何使用來自Rails 4.1並升級到Rails 4.2的assert_select的人。

在4.1這個工作:

my_url = "/my_results?search=hello" 
my_text = "(My Results)" 
assert_select 'a[href=?]', my_url, my_text 

在4.2這給了一個錯誤:「棄用警告:斷言不是因爲無效的CSS選擇器的運行意想不到的‘(’後「[:平等,」 \ 「/ my_results \」 「]」「

我改變了語法,這和它的工作!:

assert_select 'a[href=?]', my_url, { text: my_text } 

艾略特答案上面幫我,謝謝:)

+0

搜索這個問題的答案無處不在。感謝張貼這! – Kelly

+0

謝謝。在4.2中,第二個參數'my_url'必須是一個字符串。所以,如果你正在檢查一個整數,記得使用'to_s'。例如'assert_select'a [foo-id =?]',foo.id.to_s' –

0
assert_select 'a' do |link| 
    expect(link.attr('href').to_s).to eq expected_url 
end 
0

問題專門問道:「你怎麼測試[元素]的屬性」其他的答案在這裏的方式,在當前的Rails/MINITEST不再工作使用assert_select

。按本issue,關於屬性內容的斷言現在使用這種更XPath的Y「匹配」的語法:

assert_select "a:match('href', ?)", /jm3\.net/ 
相關問題