簡體   English   中英

使用web_steps.rb進行黃瓜聲明式步驟定義

[英]cucumber declarative step definitions using web_steps.rb

當將Cucumber用於BDD(CS169.x1@edX)時,響應於執行“ cucumber features / filter_movie_list.feature”,將返回以下消息:

When I uncheck the following ratings: G, PG-13                 # features/step_definitions/movie_steps.rb:44
  Undefined step: "When I uncheck "ratings_G"" (Cucumber::Undefined)
  ./features/step_definitions/movie_steps.rb:52:in `block (2 levels) in <top (required)>'
  ./features/step_definitions/movie_steps.rb:49:in `each'
  ./features/step_definitions/movie_steps.rb:49:in `/I (un)?check the following ratings: (.*)/'
  features/filter_movie_list.feature:30:in `When I uncheck the following ratings: G, PG-13'
...
You can implement step definitions for undefined steps with these snippets:

When /^When I uncheck "(.*?)"$/ do |arg1|
  pending # express the regexp above with the code you wish you had
end

有問題的特定配置在'features / filter_movie_list.feature'中有一個聲明性步驟,內容為:

當我取消選中以下評分時:G,PG-13

嘗試在“功能/step_definitions/movie_steps.rb”中實現聲明性步驟(以重用“功能/step_definitions/web_steps.rb”的命令性步驟),如下所示:

When /I (un)?check the following ratings: (.*)/ do |uncheck, rating_list|
  step "When I uncheck \"ratings_G\""

和一個有效的'features / step_definitions / web_steps.rb'文件(即在安裝gem后由'rails generatecumulage_rails_training_wheels:install'創建的文件)具有強制性的默認步驟,例如:

When /^(?:|I )uncheck "([^"]*)"$/ do |field|
  check(field)
end

另外,由於向“功能/step_definitions/movie_steps.rb”添加When I uncheck "ratings_G"成功,因此黃瓜似乎可以訪問“ features / step_definitions / web_steps.rb”。 任何人都不知道可能會導致這種行為的原因嗎? 聲明性步驟的實現甚至已經簡化,因此不會發生任何變量替換。

我試過了:

When I uncheck the following ratings: "G, PG-13"

然后在步驟文件中:

When /^I uncheck the following ratings: "([^"]*)"$/ do |arg1|
  puts "arg1: #{arg1}"
end

..似乎為我工作。

問題

問題在於您如何嘗試從另一個步驟調用一個步驟。

代碼的第二行:

When /I (un)?check the following ratings: (.*)/ do |uncheck, rating_list|
  step "When I uncheck \"ratings_G\""

正在尋找類似的步驟定義:

When /^When (?:|I )uncheck "([^"]*)"$/ do |field|
  check(field)
end

注意,它正在步驟正則表達式中尋找“何時”。 這種可能性不大,因此存在未定義的步進錯誤。

要從某個步驟調用該步驟,您需要執行以下操作之一:

1)使用step並確保不包括Given / When / Then關鍵字:

When /I (un)?check the following ratings: (.*)/ do |uncheck, rating_list|
  step "I uncheck \"ratings_G\""

2)或者,如果您希望添加“給定/何時/然后”,請改用以下steps

When /I (un)?check the following ratings: (.*)/ do |uncheck, rating_list|
  steps %Q{
    When I uncheck "ratings_G"
  }

請參閱黃瓜Wiki

在這種情況下,所提供的課程資料說明了差異,但並未使我們在非常相似的兩個步驟之間進行划分; 將“當我取消選中以下項...時”更改為“當取消選中以下項...時”可以使web_steps.rb返回范圍並根據需要執行(先前鏈接的發布, 鏈接:黃瓜矛盾的錯誤消息 ,還有更多內容)細節)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM