2013-08-20 41 views
15

Tôi có định nghĩa bước mà tôi muốn có tham số tùy chọn. Tôi tin rằng một ví dụ của hai cuộc gọi đến bước này giải thích tốt hơn bất cứ điều gì khác những gì tôi sau.Tham số tùy chọn trong dưa chuột

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

Trong trường hợp đầu tiên, tôi muốn sử dụng địa chỉ email mặc định.

Cách tốt nhất để xác định bước này là gì? Tôi không có guru regexp. Tôi đã cố gắng làm điều này nhưng dưa chuột đã cho tôi một lỗi liên quan đến sai lệch luận regexp:

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

Có gì sai với hai định nghĩa bước ? –

+0

Không có gì vốn có. Chỉ muốn căng cơ bắp dưa chuột của tôi và xem những gì có thể. – larryq

+0

Hai định nghĩa có nghĩa là hầu hết mã trùng lặp thời gian, vì vậy điều khoản về mặt lý thuyết có điều kiện là tốt hơn. – Smar

Trả lời

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 

đầu ra

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

Điều đó hoạt động tốt, và cảm ơn vì nó, nhưng tôi có thể hỏi những gì '?:' Bit không? – larryq

+3

'?:' Là một [nhóm không chụp] (http://www.regular-expressions.info/refadv.html). –

0

@larryq, bạn đã bỏ lỡ e gần gũi hơn với các giải pháp hơn bạn nghĩ ...

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 

đầu ra:

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 
Các vấn đề liên quan