使用Webrat检查列表具有特定数量的li元素

时间:2010-07-08 08:08:19

标签: ruby-on-rails ruby cucumber webrat

对于黄瓜步骤,我知道特定的ul元素应该有5个孩子li。我怎样才能用Webrat断言?

我猜可能有类似的东西:

Then ".selector" should have n ".another-selector"

或者

Then I should see n ".selector" within ".another.selector"

有没有这样的东西浮动,或者我应该自定义步骤?

谢谢!


更新

我采用了以下方法,这似乎工作正常:

# Check there are the correct number of items
assert_have_selector("#activity ol li:nth-child(5)")
assert_have_no_selector("#activity ol li:nth-child(6)")

另一次更新:

我对Matt提供的内容做了一些改动:

Then /^I should see ([0-9]+) "([^\"]*)"$/ do |count, selector|
  response.body.should have_selector(selector, :count => count.to_i)
end

努力享受!

1 个答案:

答案 0 :(得分:1)

我就是这样做的:

Then /^"([^\"]*)" should have ([0-9]+) "([^\"]*)"$/ do |selector1, count, selector2|
  within(selector1) do |content|
       content.should have_selector(selector2, :count => count.to_i)
  end
end

那诀窍呢?

Matta的