Необязательный параметр в огурце
У меня есть определение шага, в котором я хотел бы иметь необязательный параметр. Я считаю, что пример двух вызовов этого шага объясняет лучше, чем что-либо еще, что мне нужно.
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
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
Ответ 2
@larryq, вы были ближе к решению, чем вы думали...
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