2011-04-11 32 views
0

是這樣的:是否可以使用黃瓜方案作爲函數?

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" 

我想創建測試類別然後創建測試用例的「一步一步」過程。這可能沒有做「給我創建一個測試類」,然後做一個工廠呢?

回答

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" 

然後你不需要把你的支持代碼分解成許多微小的方法。

+0

那麼在你的黃瓜TestCategory類中如何創建方法呢? – corroded 2011-04-11 10:47:41

+0

更新了答案.. – 2011-04-11 10:58:39

+0

我可以做一些類似於「給定場景:創建一個測試類別」的運行「? – corroded 2011-04-12 11:50:30

相關問題