[英]How to create a Cucumber step definition for a “edit” path?
我正在嘗試學習如何使用Cucumber並使用以下場景創建步驟(我有一個名為“Vegetable”的模型,並且我添加了一個名為“color”的新屬性):
Scenario: add color to existing vegetable
When I go to the edit page for "Potato"
And I fill in "Color" with "Brown"
And I press "Update Vegetable Info"
Then the color of "Potato" should be "Brown"
我目前正在使用“訓練輪”,所以我有一個網頁步驟(web_steps.rb):
When /^(?:|I )go to (.+)?/ do |page_name|
visit path_to(page_name)
end
現在我了解這是如何工作的簡單頁面導航,如“當我去蔬菜主頁。” 我所要做的就是添加paths.rb的路徑:
When /^the vegetable home page/
'/vegetables'
但是,通過上面的例子,我需要使用特定的蔬菜“/ vegetables / 1”(Potato url)去特定的路徑。
我不知道該怎么做,所以我嘗試創建了自己的步驟:
When /I go to the edit page for "(.*)"/ do |vegetable_name|
flunk "Unimplemented"
end
但我得到錯誤:
Ambiguous match of "I go to the edit page for "Potato"":
features/step_definitions/vegetable_steps.rb:15:in `/go to the edit page for "(.*)"/'
features/step_definitions/web_steps.rb:48:in `/^(?:|I )go to (.+)$/'
You can run again with --guess to make Cucumber be more smart about it
(Cucumber::Ambiguous)
這是我應該這樣做的嗎? 或者我只是使用web_steps“轉到”步驟並在paths.rb文件中以某種方式提供id? 我只想弄清楚如何在閱讀各種Cucumber教程后開始這個。
正如DVG所說,這是因為我在兩個地方有匹配的步驟,我收到了錯誤。 要回答我自己的問題,我可以依靠已經提供的“web_step”:
When /^(?:|I )go to (.+)?/ do |page_name|
visit path_to(page_name)
end
一旦我將以下代碼添加到paths.rb:
def path_to(page_name)
case page_name
when /^the edit page for "(.*)"$/
edit_vegetable_path(Vegetable.find_by_name($1))
我能夠讓它正常工作。
問題是你匹配第二步的地方。 步驟定義是全局的。 因此,您必須更改功能以使用您沒有寫入兩個位置的步驟或刪除冗余步驟。
此外,您的功能是以非常低級別的ui-details方式編寫的。 這使您的功能變得脆弱,除非您的業務需求發生變化,否則您的黃瓜規格永遠不會改變。 考慮嘗試
Given a vegetable named "Potato"
When I mark the vegetables color as "Brown"
Then the Potato should be "Brown"
通過這種方式,您可以在不改變規范的情況下自由地試驗表單,這就是重點。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.