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

时间:2017-08-28 14:43:10

标签: ruby-on-rails minitest

我想检查导航中包含的每个链接与正则表达式。在使用这样的代码之前,我已经为各种链接检查了相同的导航:

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

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

assert_select "nav.active" do |nav|
  #assert_select "a[href=?]", /.+/
  #assert_select nav, "a[href=?]", /foo/, count: 1
end

哪个失败并输出:

Minitest::Assertion:  Expected exactly 1 element matching "a[href=/.+/]", found 0..

Minitest::Assertion:  Expected exactly 1 element matching "a[href=/foo/]", found 0..

我做错了什么?

1 个答案:

答案 0 :(得分:0)

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

assert_select "input:match('value',?)", /Clear search:/, count: 0

所以以下内容应该有效。

#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

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