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

c9qzyr3d  于 2023-10-21  发布在  Ruby
关注(0)|答案(7)|浏览(128)

我的页面应该包含一个看起来像<a href="/desired_path/1">Click to go</a>的链接。
如何使用assert_select测试它?我想检查是否存在带有href="/desired_path/1"a标记。如何测试标签的属性?
是否有任何资源可以解释如何使用assert_select?我阅读了指南和API文档,但没有弄清楚。有没有推荐的更好的方法来做到这一点?
我正在使用Rails 3并使用内置的测试框架。
谢谢.

webghufk

webghufk1#

在assert_select中,您还可以对属性值使用问号,然后在后面跟一个要匹配的字符串。
assert_select "a[href=?]", "/desired_path/1"
还有另一种更友好的方法来使用assert_select,特别是当你想匹配部分字符串或正则表达式模式时:
assert_select "a", :href => /acebook\.com\/share/

2vuwiymt

2vuwiymt2#

下面是如何使用assert_selectAssert链接的一些内容。第二个参数可以是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文档中提取的示例(参见文件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
uttx8gqw

uttx8gqw3#

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

7jmck4yq4#

这个问题具体地问:“你如何测试[一个元素]的属性?”
这里的其他答案使用assert_select的方式在当前的Rails / MiniTest中不再起作用。根据这个issue,关于属性内容的Assert现在使用这个更Xpath-y的“match”语法:

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

ffdz8vbo5#

对于任何使用来自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选择器无效,Assert未运行。在“[:equal,“"/my_results""不”“之后出现意外”(“
我改变了语法,它的工作!:

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

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

tktrz96b

tktrz96b6#

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

wrrgggsh7#

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

如果你正在向I18n传递变量:

en.yml:
en:
  objects:
    cool_text: Im the %{variable}

assert_selector :link, I18n.t("objects.cool_text", variable: @object.attribute), href: url_path

regulat string no I18n:

assert_selector :link, "Im the path", href: url_path

相关问题