2013-08-20 6 views
15

私はオプションのパラメータを持っているステップ定義を持っています。私は、このステップへの2回の呼び出しの例が、私が何をしているのか他の何よりも良く説明していると思います。キュウリの省略可能なパラメータ

I check the favorite color count 
I check the favorite color count for email address '[email protected]' 

最初の例では、デフォルトのメールアドレスを使用したいと思います。

この手順を定義するにはどうすればよいですか?私は正規表現の専門家ではない。私はこれをやってみましたが、キュウリは私の正規表現の引数の不一致に関するエラーを与えた:

Then(/^I check the favorite color count (for email address "([^"]*))*"$/) do |email = "[email protected]"| 
+1

何2つの段階の定義と間違っています? –

+0

本質的に何もありません。私のキュウリの筋肉を伸ばすことができ、可能なものを見たいだけです。 – larryq

+0

2つの定義は、ほとんどの場合、重複したコードを意味します。したがって、理論的に条件付きの節が優れています。 – Smar

答えて

27

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 '[email protected]' 

optional_steps.rb

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

出力

Feature: Optional parameter 

    Scenario: Use optional parameter 
    When I check the favorite color count 
     using [email protected] 
    When I check the favorite color count for email address '[email protected]' 
     using '[email protected]' 

1 scenario (1 passed) 
2 steps (2 passed) 
0m0.047s 
+0

それは素晴らしい作品です、そしてありがたいですが、 '?:'ビットが何をしているのか尋ねてもいいですか? – larryq

+3

'?:'は[非捕捉グループ](http://www.regular-expressions.info/refadv.html)です。 –

0

@larryq、you wer電子あなたが思ったより解に近い...

optional.feature:

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 '[email protected]' 
    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 '[email protected]' 
     using '[email protected]' 
    Then foo                  

2 scenarios (2 passed) 
6 steps (6 passed) 
0m9.733s