我如何测试RSpec匹配器has_content在页面上找到的匹配数?

时间:2012-07-13 18:01:11

标签: rspec

我基本上尝试获取“has_content?”的功能。或“should have_content”匹配器选项:count为has_css提供?匹配器。

1 个答案:

答案 0 :(得分:2)

你可以尝试类似所有('a')。长度

以下是几个例子:

describe 'my awesome page' do
  before :each do
    visit root_path
  end

  # css just for querying DOM
  it 'number of titles should be huge' do
    page.all(:css,'h2.awesome_header').length.should have_at_least(3).items
  end
  it 'has exactly 4 div elements' do
    page.all(:css,'div.awesome_class').length.should == 4
  end

  # For querying content you can try :xpath instead :css. 
  it 'has paragraps with text' do
    page.all(:xpath, '//div[contains(., "t")]').length.should == 9
  end
end

<强>更新

我找到更优雅的方式来做到这一点。 在此处查看文档:{​​{3}} 这是工作示例

page.all(:css, 'p', :text => /.*wellcome.*/).length.should == 2