2012-03-16 23 views
0

我要測試的類方法,得到整數作爲參數:黃瓜輪廓式的談話

def step(response) 
    if response < 10 

創建黃瓜場景:

Scenario Outline: submit guess 
    Given the code "<code>" 
    When I response "<answer>" 
    Then the result should be "<result>" 

    Scenarios: level one 
    | code | answer | result | 
    | 1 | 2 | 3 | 
    | 5 | 4 | 9 | 

和步difinitions:

When /^I response "([^"]*)"$/ do | response | 
    @result = @game.step(response) 
end 

當我運行測試時,我得到錯誤,因爲黃瓜將參數傳遞給我的方法作爲字符串。

我該如何解決?

我可以修復類方法代碼:

def step(response) 
    response = response.to_i 
    if response < 10 

但它會破壞我的所有現有的代碼。

+0

看到的 - http://www.engineyard.com/blog/2009/cucumber-step-argument-transforms/ – iafonov 2012-03-16 08:39:54

回答

1

黃瓜總是將字符串參數傳遞給你的步驟定義,它是步驟定義的責任,將參數轉換爲適當的類型。你需要這樣做:

When /^I response "([^"]*)"$/ do | response | 
    @result = @game.step(response.to_i) 
end 
+0

感謝。爲我感到羞恥。 – demas 2012-03-16 08:56:01