是否可以使用黄瓜方案作为功能?

时间:2011-04-11 07:09:56

标签: cucumber capybara factory-bot

类似的东西:

Scenario: Create a Test Category
  Given I am on the regression test test cases page
  When I follow "New Category"
  And I fill in "Name" with "Test Category"
  And I press "Add Category"
  Then I should see "Test Category" within ".test-categories-list"

Scenario: Add a Test Case
  Given I "Create a Test Category"

我想创建测试类别然后创建测试用例的“逐步”过程。如果没有“给定我创建一个测试类别”然后在它上面做一个工厂,这可能吗?

1 个答案:

答案 0 :(得分:0)

在这种情况下,您应该像处理生产代码一样处理测试代码并将其封装在类中。 然后,您可以轻松地在任何需要的地方重复使用它。

为了便于说明:

class TestCategory
  def create(values)
    follow_new_category
    fill_in_name
    press_add
  end

  def follow_new_category
  end

  def fill_in_name
  end

  def press_add
  end
end

然后,您可以从步骤中调用这些方法:

When /I fill in "Name" with "(.*)"/ do |category_name| 
  TestCategory.new.fill_in_name(category_name)
end

(您可能希望以其他方式管理TestCategory类的生命周期,而不是每次都实例化一个新的...)

但是,您的步骤非常迫切,这不是一种好的做法。如果说出这样的话会更好:

Scenario: Create a Test Category
  Given I am on the regression test test cases page
  When I add a test category
  Then I should see "Test Category" within ".test-categories-list"

然后,您不需要将支持代码分解为如此多的小方法。

相关问题