有没有办法取代Gherkin参数的文本,并将其传递给相应的步骤定义?
例如,假设我有下面的Gherkin步骤进行匹配转换:
Given I select "any option" from the "Choose an option" dropdown
DROPDOWN_OPTION = Transform /^any option$/ do |str|
str = all("li", :minimum => 1).sample.text
end如果它选择的li是“设置”,我希望执行该步骤后的结果如下所示:
Given I select "settings" from the "Choose an option" dropdown我意识到,这并不是编写Gherkin的理想方法(甚至在一般情况下也不是测试),但不幸的是,这是我一直坚持的一个限制。
有人能告诉我吗?
发布于 2014-09-04 17:00:01
这就是你要问的吗?
/特性/步骤_定义/下拉_step_
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/特征/下拉选项
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这样做是将每个匹配组传递到下面的块中,允许您设置实例变量之类的内容并对它们执行操作。因此,在我的第一步中,它从regex中创建了一个名为@option_selected的实例变量。第二步告诉#go这个实例变量,第三步确保它是#went?。最后,第四步确保即使它消失了,它仍然是相同的“选项”。
这显然是一个非常通用的例子,只是为了说明特性和步骤定义是如何工作的。它假设了很多事情,但基本上它可以处理这样的类
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
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输出
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当捕获组匹配一个转换器时,转换将发生,这将用管道代替原来的捕获。
特性定义是为了测试目的而修正的。他们的意思是言简意赅,由匹配者来处理。它们并不是动态实现的,因为这样做会偏离它们的真正目的。
发布于 2014-09-04 20:43:02
与其重写源Gherkin,似乎您的主要目标是操纵您的结果,而不是您的输入。如果这是您的目标,只需创建一个自定义格式化程序。通过重写AfterStep,您可以将值插入结果并交流所需内容。
发布于 2014-09-04 14:58:22
尽管黄瓜不关心关键字的顺序,但惯例是按照以下步骤编写步骤:
Given the context
When action
Then expectation 因此,要改变您的场景,我会写:
Given I have options
When I choose "whatever" dropdown
Then I expect ...https://stackoverflow.com/questions/25668384
复制相似问题