功能中的黄瓜HTML标记

时间:2011-08-02 18:31:39

标签: ruby cucumber

我有一个黄瓜场景,我想测试HTML标签。

Scenario: enter words
    Given I enter "cat,dog"
    When I set tag to "li" and the class to "word"
    Then I should see "<li class=\"word\">cat</li>"
    And I should see "<li class=\"word\">dog</li>"

这是编写此方案的正确方法吗?

1 个答案:

答案 0 :(得分:2)

您的目标应该是用简单的英语阅读您的场景。如果我不是开发人员,那么这个场景对我来说没有多大意义。你可以这样做:

Then I should see cat within a word list element

这一步骤将是:

Then /^(?:|I )should see "([^"]*)" within (.*)$/ do |text, parent|
  with_scope(parent) do
    if page.respond_to? :should
      page.should have_content(text)
    else
      assert page.has_content?(text)
    end
  end
end

黄瓜发生器应该已经提供了with_scope方法,但无论如何它都是:

module WithinHelpers
  def with_scope(locator)
    locator ? within(*selector_for(locator)) { yield } : yield
  end
end
World(WithinHelpers)

只需确保将选择器添加到locors的case语句中的features / support / selectors中的selectors.rb中:

module HtmlSelectorsHelpers

  def selector_for(locator)
    case locator

    when ' a word list element'
      'li.word'