這裏是你如何可以斷言數關於使用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
這不再Rails中的5部作品,也不是,我相信,在導軌4.請參閱[我的答案]( https://stackoverflow.com/a/48394216/187833)爲當前的語法。 – jm3