這是你問
/features/step_definitions/drop_down_steps.rb
Given /^I select (\w+) from the Choose an option dropdown$/ do |opt|
#do what ever you need to here with opt
instance_variable_set("@option_selected",DropDownOption.new(opt))
end
When /^I make it go$/ do
#make it go
@option_selected.go
end
Then /^I expect that it went$/ do
#test that it went with the opt selected
@option_selected.went?
end
Then /^I expect it is still (\w+)$/ do |opt|
@option_selected.selected == opt
end
/features/drop_down_option.feature
Feature: DropDownOption
This is to test that I can send a DropDownOption away
but it will still be itself
Scenario: Selected Stuff
Given I select stuff from the Choose an option dropdown
When I make it go
Then I expect that it went
Then I expect it is still stuff
這樣做是什麼每個匹配組傳遞到塊之下,允許您設置之類的實例變量,並對其執行操作。因此,在我的第一步中,它會從「stuff」正則表達式匹配中創建一個名爲@option_selected
的實例變量。第二步告訴這個實例變量爲#go
,第三步確保它爲#went?
。最後,第四步確保即使它消失,它仍然是相同的「選項」。
這顯然是一個非常通用的例子,只是爲了展示功能和步驟定義的工作方式。它假設了很多東西,但基本上它會用類,如果你想改變的東西,那麼它必須作爲一個捕獲組
Feature: RandomString
Scenario: With a Random String
Given I type "random string" into the title field
Then I want it to be a String
Transform /^I type ? "(.*)"$/ do |str|
rand(36**15).to_s(36)
end
Given /^(I type "(?:.*)") into the title field$/ do |str|
instance_variable_set("@random_string",str)
end
Then /^I want it to be a String$/ do
puts @random_string
expect(@random_string).to be_kind_of(String)
end
像這樣工作
class DropDownOption
def initialize(opt)
@opt = opt
@is_here = true
end
def go
@is_here = false
end
def selected
@opt
end
def is_here?
@is_here
end
def went?
!is_here?
end
end
更新
輸出
Feature: RandomString
Scenario: With a Random String # features\random_string.feature:2
Given I type "random string" into the title field # features/step_definitions/random_steps.rb:4
Then I want it to be a String # features/step_definitions/random_steps.rb:7
qxv75si91k2u10s #this is the transformed string it is random but will not alter the feature step definition
Whe捕獲組匹配一個轉換將發生的變換器,這將被用來代替原始捕獲。
功能定義旨在爲測試目的而修復。他們的意思是措辭清晰,並由匹配者處理。它們並不意味着要被動態地實現,因爲這會帶走它們的真正目的。
不知道我明白,因爲'黃瓜'是如何工作的。 [步驟定義](http://cukes.info/step-definitions.html)一致使用正則表達式,因爲它們的真正意圖是允許這種類型的行爲。 – engineersmnky 2014-09-04 14:54:24
也許它只是不適合我嗎? 我最初有這套作爲變換。類似於: 'DROPDOWN_OPTION =轉換/ ^任意選項$/do | str | STR =所有(「裏」,:最小=> 1)。樣品 end' 但是被執行此步驟時,結果仍顯示爲: '鑑於我選擇「的任何選項」的「選擇選項「下拉列表」 而不是: '鑑於我從「選擇選項」下拉列表中選擇了「選定的選項」 – 2014-09-04 16:21:24