25

我有一个步骤定义,我想要一个可选参数。我相信两次调用此步骤的示例比其他任何事情都更能解释我所追求的。

I check the favorite color count
I check the favorite color count for email address 'john@anywhere.com'

首先,我想使用默认电子邮件地址。

定义此步骤的好方法是什么?我不是正则表达式大师。我试过这样做,但黄瓜给了我一个关于正则表达式参数不匹配的错误:

Then(/^I check the favorite color count (for email address "([^"]*))*"$/) do  |email = "default_email@somewhere.com"|  
4

2 回答 2

40

可选功能:

Feature: Optional parameter

  Scenario: Use optional parameter
    When I check the favorite color count
    When I check the favorite color count for email address 'john@anywhere.com'

optional_steps.rb

When /^I check the favorite color count(?: for email address (.*))?$/ do |email|
  email ||= "default@domain.com"
  puts 'using ' + email
end

输出

Feature: Optional parameter

  Scenario: Use optional parameter
    When I check the favorite color count
      using default@domain.com
    When I check the favorite color count for email address 'john@anywhere.com'
      using 'john@anywhere.com'

1 scenario (1 passed)
2 steps (2 passed)
0m0.047s
于 2013-08-21T07:46:40.403 回答
0

@larryq,您比您想象的更接近解决方案...

可选功能:

Feature: optional parameter

Scenario: Parameter is not given
    Given xyz
    When I check the favorite color count
    Then foo

Scenario: Parameter is given
    Given xyz
    When I check the favorite color count for email address 'john@anywhere.com'
    Then foo

optional_steps.rb

When /^I check the favorite color count( for email address \'(.*)\'|)$/ do |_, email|
    puts "using '#{email}'"
end

Given /^xyz$/ do
end

Then /^foo$/ do
end

输出:

Feature: optional parameter

Scenario: Parameter is not given
    Given xyz
    When I check the favorite color count
        using ''
    Then foo

Scenario: Parameter is given
    Given xyz                                                                   
    When I check the favorite color count for email address 'john@anywhere.com'
        using 'john@anywhere.com'
    Then foo                                                                    

2 scenarios (2 passed)
6 steps (6 passed)
0m9.733s
于 2017-07-11T14:52:12.877 回答