Rspec内容与文本

时间:2012-10-13 00:20:49

标签: ruby-on-rails rspec capybara

我遇到了几个例子,人们用page.should have_selector "title", content: "my awsome content"测试html元素的内容,但这似乎总是过去。正确的通话似乎是page.should have_selector "title", text: "my awsome content"(通知text:而不是content:)。

此Rspec调用中的content:选择器是什么?

1 个答案:

答案 0 :(得分:1)

简短回答:

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? false捕获然后返回def assert_selector(*args) synchronize do result = all(*args) result.matches_count? or raise Capybara::ExpectationNotMet, result.failure_message end return true end 。)

all

# @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 方法返回与查询匹配的所有结果。我会在This找到有关可接受选项的文档:

content

我们可以看到{{1}}未列为有效选项,因此只会被忽略。