简答:
have_selector 不支持:content 选项。
长答案:
Rspec 创建动态方法,将[].should be_empty 之类的调用转换为[].empty?.should == true,在您的情况下,将page.should have_selector "title", content: "my awesome content" 转换为page.has_selector?("title, content: "my awesome content").should == true。
Capybara 提供了has_selector? 方法,它基本上只是将其参数传递给它的test/unit 样式断言assert_selector:
def has_selector?(*args)
assert_selector(*args)
rescue Capybara::ExpectationNotMet
return false
end
assert_selector 然后再次将其参数传递给 Capybara finder 方法all(),如果找到任何匹配项则返回true,如果没有则引发异常(has_selector? 捕获然后返回@987654336 @ 与)。
def assert_selector(*args)
synchronize do
result = all(*args)
result.matches_count? or raise Capybara::ExpectationNotMet, result.failure_message
end
return true
end
all 方法返回与查询匹配的所有结果。 This 是我们可以找到有关可接受选项的文档的地方:
# @overload all([kind], locator, options)
# @param [:css, :xpath] kind The type of selector
# @param [String] locator The selector
# @option options [String, Regexp] text Only find elements which contain this text or match this regexp
# @option options [Boolean] visible Only find elements that are visible on the page. Setting this to false
# (the default, unless Capybara.ignore_hidden_elements = true), finds
# invisible _and_ visible elements.
# @return [Array[Capybara::Element]] The found elements
我们可以看到content 没有被列为有效选项,因此被忽略了。