2013-08-01 91 views
1

我有大約20場景使用相同的整數來指定tableviewcell的行號。產品最近發生了一些變化,我只能通過更改行號來更改所有20種情況。目前,我寧願不作出具體該行的一步,因爲我已經有大約30個不同的行處理:在黃瓜中使用常量?

我寧願做:

Given /^I am on the page labeled "([^"]+)" on row number (\d*)$/ do |page_name, row_number| 
    steps %Q{ 
     When I scroll down #{row_number} rows in table "LeftMenu" 
     And I touch the TableViewCell marked "#{page_name}" 
    } 
end 

,而不是:

Then /^I select specificName$/ do 
    scroll_down(24) 
end 

我可以在黃瓜中使用常量嗎?事情是這樣的:

const ROW_NUM 24 

Then I select "tableViewCellName" at row number ROW_NUM 

感謝

回答

2

您不能直接傳遞常數的步驟定義。但是,您可以通過步驟定義檢查傳遞的值是否與常量匹配。

例如,通過以下幾個步驟:

ROW_NUM = 24 

Then /I select "tableViewCellName" at row number (.+)/ do |row| 
    row = (Kernel.const_get row rescue row.to_i) 
    p row 
end 

您可以與ROW_NUM 「常數」 稱之爲:

Then I select "tableViewCellName" at row number ROW_NUM 
#=> 24 

或與特定行號:

Then I select "tableViewCellName" at row number 1 
#=> 1 
2

有幾種解決方法,並非所有解決方案都適合您的情況

  1. 可以組所有的測試用例一行到一個大的測試案例,然後調用

    When I test row 23

  2. 你可以有一個步驟,它將行號保存到一個變量

    When I prepare to test row 23 
    And then I do something with the row I am testing 
    Then something is displayed in the row I am testing

  3. 使用表

    When I test the following rows with data: 
        | 23 | something | something else | 
        | 26 | something | something else | 
        | 28 | something | something else |