2013-08-20 7 views
15

में वैकल्पिक पैरामीटर मेरे पास एक चरण परिभाषा है जिसमें मैं एक वैकल्पिक पैरामीटर रखना चाहता हूं। मेरा मानना ​​है कि इस चरण में दो कॉलों का एक उदाहरण जो कुछ भी है उसके बाद बेहतर बताता है।ककड़ी

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

पहले उदाहरण में, मैं एक डिफ़ॉल्ट ईमेल पता का उपयोग करना चाहता हूं।

इस चरण को परिभाषित करने का एक अच्छा तरीका क्या है? मैं कोई regexp गुरु नहीं हूँ। मैं लेकिन ककड़ी ऐसा करने की कोशिश की मुझे regexp तर्क बेमेल के बारे में एक त्रुटि दिया:

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

क्या दो चरणों वाली परिभाषा के साथ गलत क्या है ? –

+0

स्वाभाविक रूप से कुछ भी नहीं। बस मेरी ककड़ी की मांसपेशियों को फैलाएं और देखें कि क्या संभव है। – larryq

+0

दो परिभाषाओं का अर्थ अधिकांश समय डुप्लिकेट कोड है, इसलिए सैद्धांतिक रूप से सशर्त खंड बेहतर है। – 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

@ लारीक, आप 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 
संबंधित मुद्दे