替换Gherkin参数字符串

时间:2014-09-04 14:41:59

标签: ruby cucumber capybara gherkin

有没有办法替换已经传递给相应步骤定义的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的理想方法(或者甚至是一般的测试,但不幸的是,这是我坚持的限制。

有人可以提供建议吗?

3 个答案:

答案 0 :(得分: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?。最后,第四步确保即使它消失了它仍然是相同的“选项”。

这显然是一个非常通用的示例,仅用于说明功能和步骤定义的工作原理。它假定了很多东西,但基本上它可以用于像这样的类

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

当捕获组与变换器匹配时,将进行转换,并且将使用管道输入来代替原始捕获。

特征定义旨在用于测试目的。它们应该是明确的措辞,并由匹配者处理。它们并不是动态实现的,因为这会剥夺它们的真正目的。

答案 1 :(得分:1)

而不是重写您的源Gherkin,似乎您的主要目标实际上是操纵您的结果,而不是您的输入。如果这是您的目标,只需创建custom formatter即可。通过覆盖AfterStep,您可以将值插入结果中并传达您需要的内容。

答案 2 :(得分:0)

虽然,黄瓜并不关心关键字的顺序,但惯例是根据以下内容编写步骤:

Given the context
When action
Then expectation 

所以为了改变你的场景我会写:

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