这是你问
/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