黄瓜中的可选参数

lar*_*ryq 23 cucumber

我有一个步骤定义,我希望有一个可选参数.我相信这个步骤的两个调用的例子比我所追求的更好.

I check the favorite color count
I check the favorite color count for email address 'john@anywhere.com'
Run Code Online (Sandbox Code Playgroud)

在第一个例子中,我想使用默认的电子邮件地址.

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

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

bas*_*302 34

optional.feature:

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'
Run Code Online (Sandbox Code Playgroud)

optional_steps.rb

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

产量

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
Run Code Online (Sandbox Code Playgroud)

  • `?:`是[非捕获组](http://www.regular-expressions.info/refadv.html). (5认同)