繁体   English   中英

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

[英]How to use assert_select to test for presence of a given link?

我的页面应该包含一个类似于<a href="/desired_path/1">Click to go</a>的链接。

您将如何使用 assert_select 进行测试? 我想检查是否存在带有href="/desired_path/1"a标签。 如何测试标签的属性?

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

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

谢谢。

在assert_select中,也可以对属性值使用问号,然后跟一个字符串进行匹配。

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

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

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

以下是如何使用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

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

这个问题特别问,“你如何测试[一个元素]的属性?”

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

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

对于任何使用来自 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 选择器,断言未运行。意外的 '(' after '[:equal, "\"/my_results\""]'"

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

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

上面的艾略特回答对我有帮助,谢谢:)

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM