如何使用assert_select来测试给定链接的存在?

时间:2011-08-17 19:43:59

标签: ruby-on-rails ruby-on-rails-3 testing minitest testunit

我的网页应包含类似<a href="/desired_path/1">Click to go</a>的链接。

你如何使用assert_select测试?我想检查a标记是否存在href="/desired_path/1"。如何测试标签的属性?

是否有任何资源可以解释如何使用assert_select?我阅读了指南和API文档,但没有弄清楚。有没有建议更好的方法来做到这一点?

我正在使用Rails 3并使用内置的测试框架。

感谢。

6 个答案:

答案 0 :(得分:27)

在assert_select中,您还可以使用问号作为属性值,然后使用字符串进行匹配。

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

还有另一种方法可以使用更友好的assert_select,特别是如果你想匹配部分字符串或正则表达式模式:

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

答案 1 :(得分:17)

以下是使用assert_select声明链接的一些内容的方法。第二个参数可以是String或Regexp来测试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 docs中的示例(参见文件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

答案 2 :(得分:16)

您可以将任何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

答案 3 :(得分:2)

对于任何使用来自Rails 4.1的assert_select并升级到Rails 4.2的人。

在4.1中,这有效:

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

在4.2中,这给出了一个错误:&#34; DEPRECATION警告:由于css选择器无效,因此未运行断言。意外的&#39;(&#39;在&#39; [:相等,&#34; \&#34; / my_results \&#34;&#34;]&#39;&#34;&#34;

我将语法更改为此并且有效!:

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

以上的艾略特回答帮助了我,谢谢:)。

答案 4 :(得分:2)

问题特别问:“你如何测试[元素]的属性?”

此处的其他答案使用assert_select以不再适用于当前Rails / MiniTest的方式。根据这个issue,关于属性内容的断言现在使用这个更多的Xpath-y'匹配'语法:

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

答案 5 :(得分:1)

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