2014-09-04 66 views
1

有什么方法可以替换已传递给其相应步骤定义的小黄瓜参数文本?替换黄瓜参数字符串

例如,假设我有变换的选配以下小黄瓜步:

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 

如果选择了小李的“设置”,我想执行这一步骤看起来像这样之后的结果:

Given I select "settings" from the "Choose an option" dropdown 

我意识到,这不是写小黄瓜(甚至在一般的测试,对于这个问题),但它是不幸的,我套牢了限制的理想方法。

任何人都可以建议吗?

+1

不知道我明白,因为'黄瓜'是如何工作的。 [步骤定义](http://cukes.info/step-definitions.html)一致使用正则表达式,因为它们的真正意图是允许这种类型的行为。 – engineersmnky 2014-09-04 14:54:24

+0

也许它只是不适合我吗? 我最初有这套作为变换。类似于: 'DROPDOWN_OPTION =转换/ ^任意选项$/do | str | STR =所有(“里”,:最小=> 1)。样品 end' 但是被执行此步骤时,结果仍显示为: '鉴于我选择“的任何选项”的“选择选项“下拉列表” 而不是: '鉴于我从“选择选项”下拉列表中选择了“选定的选项” – 2014-09-04 16:21:24

回答

1

这是你问

/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捕获组匹配一个转换将发生的变换器,这将被用来代替原始捕获。

功能定义旨在为测试目的而修复。他们的意思是措辞清晰,并由匹配者处理。它们并不意味着要被动态地实现,因为这会带走它们的真正目的。

+0

这并不完全是我所问的,可悲的是(尽管......我实际上很喜欢你在这里所做的一切,并有几个我想要应用的区域;这几乎不浪费时间)。 虽然我只是稍微重构了我的问题以更具体一些。我所问的基本上是,如果小黄瓜是这样的话:'鉴于我有一个随机字符串'',我怎么能打印出'鉴于我有'dj3(42 ^&e6he @ 3“'在结果? – 2014-09-04 17:21:53

+0

即如何 – 2014-09-04 17:26:18

+0

@DavidAdams我认为我现在理解你的概念,但是你希望它变成什么样的? – engineersmnky 2014-09-04 17:57:05

0

虽然,黄瓜并不关心关键字的顺序,惯例是根据下面写你的步骤:

Given the context 
When action 
Then expectation 

因此改变你的情况我会写:

Given I have options 
When I choose "whatever" dropdown 
Then I expect ... 
+0

其实,这是我们的初始方法。我把这个团队设置成像这样写Gherkin - 但不幸的是,这种方法不利于我们组织的内部结构。手动测试人员(而不是开发人员甚至自动化团队)是这些自动化测试的最终恩人,所以小黄瓜不得不被重写为更具可追溯性和特定性。 – 2014-09-04 17:09:59

1

而不是重写你的源小黄瓜,它似乎是你的主要目标是真正的操纵你的结果,而不是你的输入。如果这是您的目标,只需创建一个custom formatter。通过覆盖AfterStep,您可以将值插入到结果中并传达您所需的内容。

+0

不错,我甚至不知道这些。我通常只是使用RSpec,但我会看看这些都是关于什么的。 – engineersmnky 2014-09-04 20:44:33

+0

呃,他们让它看起来比实际上似乎更容易这是真棒,但感谢发布,因为我不会意识到有多少回叫可用 – engineersmnky 2014-09-04 21:06:48