2013-08-20 30 views
15

İsteğe bağlı bir parametrenin olmasını istediğim bir adım tanımım var. Bu adımın iki çağrısının bir örneğinin, neyin peşinde olduğumdan başka her şeyden daha iyi olduğunu düşünüyorum.Salatalıkta isteğe bağlı parametre

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

İlk olarak, varsayılan e-posta adresini kullanmak istiyorum.

Bu adımı tanımlamanın iyi bir yolu nedir? Regexp gurusu değilim. Ben salatalık ama bunu denedim regexp'in argüman uyuşmazlıkları ile ilgili olarak bana bir hata verdi:

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

Hangi iki adım tanımları nesi ? –

+0

Doğal olarak hiçbir şey. Sadece salatalık kaslarımı germek ve mümkün olanı görmek istiyorum. – larryq

+0

İki tanım, çoğunun kodu çoğaltması anlamına gelir, bu nedenle teorik olarak koşullu madde daha iyidir. – Smar

cevap

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 

çıkış

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

Bu harika çalışıyor ve bunun için teşekkürler, ama '': 'bit'in ne yaptığını sorabilir miyim? – larryq

+3

'?:' Bir [yakalama dışı grup] 'dir (http://www.regular-expressions.info/refadv.html). –

0

@larryq, sen wer e düşündüğünden daha çözüme yakın ...

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 

çıkışı:

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 
İlgili konular