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

问题描述 投票:0回答:7

我的页面应该包含一个类似

<a href="/desired_path/1">Click to go</a>
的链接。

您将如何使用assert_select 进行测试?我想检查是否存在带有

a
href="/desired_path/1"
标签。如何测试标签的属性?

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

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

谢谢。

ruby-on-rails ruby-on-rails-3 testing minitest testunit
7个回答
34
投票

在assert_select中,还可以用问号作为属性值,然后跟一个字符串来匹配。

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

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

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


21
投票

以下是如何使用

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

20
投票

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

7
投票

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

这里的其他答案使用

assert_select
的方式在当前 Rails / MiniTest 中不再有效。根据此问题,有关属性内容的断言现在使用更多 Xpath-y“匹配”语法:

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

4
投票

对于使用来自 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 中,出现错误:“弃用警告:由于 css 选择器无效,断言未运行。‘[:equal, "\"/my_results\""]'" 之后出现意外的 '('

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

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

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


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

0
投票

rails (7.0.8), selenium-webdriver (4.12.0), capybara (3.39.2)

assert_selector :link, I18n.t("en.yml path here"), href: url_path

assert_selector :link, "Im the path", href: url_path
© www.soinside.com 2019 - 2024. All rights reserved.