Rails:无法在assert_select块中使用正则表达式

myc*_*ius 1 ruby-on-rails minitest

我想根据正则表达式检查导航中包含的每个链接。在使用如下代码之前,我已经检查了相同的导航是否有各种链接:

assert_select "nav.active" do |nav|
  assert_select nav, "a[href=?]", edit_post_path(post), count: 0
end
Run Code Online (Sandbox Code Playgroud)

效果很好。我无法使用正则表达式执行类似的操作,如文档中所示。我尝试过使用这些变体(它们都故意注释掉):

assert_select "nav.active" do |nav|
  #assert_select "a[href=?]", /.+/
  #assert_select nav, "a[href=?]", /foo/, count: 1
end
Run Code Online (Sandbox Code Playgroud)

失败并输出:

Minitest::Assertion:  Expected exactly 1 element matching "a[href=/.+/]", found 0..
Run Code Online (Sandbox Code Playgroud)

或者

Minitest::Assertion:  Expected exactly 1 element matching "a[href=/foo/]", found 0..
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

小智 5

我今天在寻找类似答案时遇到了这个问题。我刚刚在测试中成功使用了这一行:

assert_select "input:match('value',?)", /Clear search:/, count: 0
Run Code Online (Sandbox Code Playgroud)

所以以下应该有效。

#assert_select "a[href=?]", /.+/
assert_select "a:match('href',?)", /.+/

#assert_select nav, "a[href=?]", /foo/, count: 1
assert_select "a:match('href',?)", /foo/, count: 1
Run Code Online (Sandbox Code Playgroud)

Rails 5 使用Nokogirl 的assert_select 实现。nav根据其文档,我认为您不需要在循环中引用。https://github.com/kaspth/rails-dom-testing/blob/master/lib/rails/dom/testing/assertions/selector_assertions.rb