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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7098499/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 01:47:21  来源:igfitidea点击:

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

ruby-on-railsruby-on-rails-3testingminitesttestunit

提问by B Seven

My page should contain a link that looks like <a href="/desired_path/1">Click to go</a>.

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

How would you test for that using assert_select? I want to check for the presence of an atag with href="/desired_path/1". How do you test the attributes of a tag?

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

Are there any resources to explain how to use assert_select? I read the Guides and API documentation, but didn't figure it out. Are there any recommended better ways of doing this?

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

I am working in Rails 3 and using the built-in test framework.

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

Thanks.

谢谢。

采纳答案by Blacksad

You can pass any CSS selector to assert_select. So to test the attribute of a tag, you use [attrname=attrvalue]:

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

回答by Chris Houhoulis

In assert_select, you can also use a question mark for the attribute value, and then follow with a string to match.

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

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

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

There's another way to use assert_select that is more friendly, especially if you want to match a partial string or regexp pattern:

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

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

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

回答by Eliot Sykes

Here is how you can assert a number of things about a link using assert_select. The 2nd argument can either be a String or a Regexp to test the href attribute against:

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

For other ways you can use assert_select, here are the examples taken from the Rails actionpack 3.2.15 docs (see file actionpack-3.2.15/lib/action_dispatch/testing/assertions/selector.rb):

对于您可以使用的其他方式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

回答by jm3

The question specifically asks, “How do you test the attributes of [an element]?”

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

The other answers here use assert_selectin ways that no longer work in current Rails / MiniTest. As per this issue, assertions about attribute contents now use this more Xpath-y ‘match' syntax:

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

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

回答by Richard

For anyone using assert_select coming from Rails 4.1 and upgrading to Rails 4.2.

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

In 4.1 this worked:

在 4.1 中,这有效:

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

In 4.2 this gave an error: "DEPRECATION WARNING: The assertion was not run because of an invalid css selector. unexpected '(' after '[:equal, "\"/my_results\""]'"

在 4.2 中,这给出了一个错误:“弃用警告:由于 css 选择器无效,断言没有运行。意外的 '(' after '[:equal, "\"/my_results\""]'"

I changed the syntax to this and it worked!:

我将语法更改为此,它起作用了!:

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

Eliots answer above helped me, thanks :)

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

回答by Sujoy Gupta

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