首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >替换Gherkin参数字符串

替换Gherkin参数字符串
EN

Stack Overflow用户
提问于 2014-09-04 14:41:59
回答 3查看 2.3K关注 0票数 1

有没有办法取代Gherkin参数的文本,并将其传递给相应的步骤定义?

例如,假设我有下面的Gherkin步骤进行匹配转换:

代码语言:javascript
复制
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是“设置”,我希望执行该步骤后的结果如下所示:

代码语言:javascript
复制
Given I select "settings" from the "Choose an option" dropdown

我意识到,这并不是编写Gherkin的理想方法(甚至在一般情况下也不是测试),但不幸的是,这是我一直坚持的一个限制。

有人能告诉我吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-09-04 17:00:01

这就是你要问的吗?

/特性/步骤_定义/下拉_step_

代码语言:javascript
复制
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

/特征/下拉选项

代码语言:javascript
复制
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?。最后,第四步确保即使它消失了,它仍然是相同的“选项”。

这显然是一个非常通用的例子,只是为了说明特性和步骤定义是如何工作的。它假设了很多事情,但基本上它可以处理这样的类

代码语言:javascript
复制
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

更新

如果您想要转换某个东西,那么它必须充当捕获组。

代码语言:javascript
复制
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

输出

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2014-09-04 20:43:02

与其重写源Gherkin,似乎您的主要目标是操纵您的结果,而不是您的输入。如果这是您的目标,只需创建一个自定义格式化程序。通过重写AfterStep,您可以将值插入结果并交流所需内容。

票数 1
EN

Stack Overflow用户

发布于 2014-09-04 14:58:22

尽管黄瓜不关心关键字的顺序,但惯例是按照以下步骤编写步骤:

代码语言:javascript
复制
Given the context
When action
Then expectation 

因此,要改变您的场景,我会写:

代码语言:javascript
复制
Given I have options
When I choose "whatever" dropdown
Then I expect ...
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25668384

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档