25

オプションのパラメーターが必要なステップ定義があります。このステップへの 2 つの呼び出しの例は、私が何を求めているかを何よりもよく説明していると思います。

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 に答える