我想检查导航中包含的每个链接与正则表达式。在使用这样的代码之前,我已经检查了相同的导航以获取各种链接:
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..
我究竟做错了什么?
我今天在寻找类似的答案时遇到了这个问题。我在测试中成功使用了这一行:
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实现。根据它的文档,我认为你不需要在循环中引用nav
。 https://github.com/kaspth/rails-dom-testing/blob/master/lib/rails/dom/testing/assertions/selector_assertions.rb