閱讀224 返回首頁    go 阿裏雲 go 技術社區[雲棲]


《Cucumber:行為驅動開發指南》——2.3 創建步驟定義

本節書摘來自異步社區《Cucumber:行為驅動開發指南》一書中的第2章,第2.3節,作者:【英】Matt Wynne , 【挪】Aslak Hellesy著,更多章節內容可以訪問雲棲社區“異步社區”公眾號查看

2.3 創建步驟定義

先不要過多考慮之前Cucumber輸出的代碼片段是什麼意思,我們先把這些代碼複製並粘貼到一個Ruby文件中。和特性文件一樣,Ruby期望在約定俗成的位置找到步驟定義:

$ mkdir features/step_definitions
現在在fetures/step_definitions目錄下創建一個名為calculator_steps.rb的文件,隻要這是一個Ruby文件,Cucumber並不介意你給這個文件起什麼名字,但這裏我們給這個文件起的名字其實不錯。接著用編輯器打開該文件並粘貼下麵的代碼片段:

下載first_taste/02/features/step_definitions/calculator_steps.rb
Given /^the input "([^"]*)"$/ do |arg1|
 pending # express the regexp above with the code you wish you had
end

When /^the calculator is run$/ do
 pending # express the regexp above with the code you wish you had
end

Then /^the output should be "([^"]*)"$/ do |arg1|
 pending # express the regexp above with the code you wish you had
end

運行cucumber,它會告訴我們下一步做什麼:

Feature: Adding

 Scenario: Add two numbers    
  Given the input "2+2"     
   TODO (Cucumber::Pending)
   ./features/step_definitions/calculator_steps.rb:2
   features/adding.feature:4
  When the calculator is run  
  Then the output should be "4" 

1 scenario (1 pending)
3 steps (2 skipped, 1 pending)
0m0.003s

場景已經從未定義(undefined)升級到了待定(pending)。這是個好消息,因為它說明Cucumber正在運行第一個步驟,不過在此過程中它撞上了我們複製並粘貼的那些步驟定義代碼中的pending標記,pending的意思是告訴Cucumber這個場景還是一個正在進行中的工作。我們需要用真正的實現替換掉這個pending標記。

注意,Cucumber報告它跳過了另外兩個步驟,隻要遇到了失敗的或者待定的步驟,Cucumber就會停止運行當前場景並跳過該場景剩餘的步驟。

下麵我們來實現第一個步驟定義。

最後更新:2017-06-05 11:34:01

  上一篇:go  《Cucumber:行為驅動開發指南》——2.4 實現第一個步驟定義
  下一篇:go  《Cucumber:行為驅動開發指南》——2.1 理解我們的目標