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

B S*_*ven 30 testing ruby-on-rails testunit minitest ruby-on-rails-3

我的页面应该包含一个看起来像的链接<a href="/desired_path/1">Click to go</a>.

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

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

我在Rails 3中工作并使用内置的测试框架.

谢谢.

Chr*_*lis 29

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

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

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

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

  • 正如@Shadwell所提到的,**第二**形式不起作用(Rails 3.2.15).不幸的是,它不会导致断言失败,我相信:href选项会被默默忽略.如果有人想为自己证明这一点,那么在:href正则表达式参数中写一些应该失败的东西,看看assert_select如何仍然通过. (6认同)
  • 第二种形式似乎只适用于元素的文本,而不适用于属性.我尝试时忽略了href条件. (4认同)
  • 在Rails 5.0中,我不得不使用``a [href]"`而不是`"a [href =?]"` (2认同)
  • 对于 Rails 5.0,此答案的后半部分仍然不正确。 (2认同)

Eli*_*kes 19

以下是如何使用链接断言链接的一些内容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' }
Run Code Online (Sandbox Code Playgroud)

对于您可以使用的其他方法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
Run Code Online (Sandbox Code Playgroud)

  • @Mohamad,我认为这会做到,每个属性都放在自己的方括号中:`assert_select 'a[href=?][data-foo="bar"]', 'http://test.host/dashboard '` (2认同)

Bla*_*sad 17

您可以将任何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
Run Code Online (Sandbox Code Playgroud)


jm3*_*jm3 6

该问题具体询问,“您如何测试 [一个元素] 的属性?”

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

assert_select "a:match('href', ?)", /jm3\.net/
Run Code Online (Sandbox Code Playgroud)