代码之家  ›  专栏  ›  技术社区  ›  dylanfm

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

  •  0
  • dylanfm  · 技术社区  · 14 年前

    对于黄瓜步骤,我知道 ul 元素应该有5个子元素 li

    我猜可能会有这样的情况:

    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)")
    

    另一个更新:

    我对马特提供的东西做了一点改动:

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

    工作是一种享受!

    1 回复  |  直到 14 年前
        1
  •  1
  •   Matta    14 年前

    我就是这样做的:

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

    这样就行了?

    马塔