I have a step definition in which I'd like to have an optional parameter. I believe an example of two calls to this step explains better than anything else what I'm after.
I check the favorite color count I check the favorite color count for email address '[email protected]'
In the first instance, I would like to use a default email address.
What's a good way of defining this step? I'm no regexp guru. I tried doing this but cucumber gave me an error regarding regexp argument mismatches:
Then(/^I check the favorite color count (for email address "([^"]*))*"$/) do |email = "[email protected]"|
What are Optional Parameters? By definition, an Optional Parameter is a handy feature that enables programmers to pass less number of parameters to a function and assign a default value.
Optional parameters are defined at the end of the parameter list, after any required parameters. If the caller provides an argument for any one of a succession of optional parameters, it must provide arguments for all preceding optional parameters. Comma-separated gaps in the argument list aren't supported.
Optional Parameters are parameters that can be specified, but are not required. This allows for functions that are more customizable, without requiring parameters that many users will not need.
By Params Keyword: You can implement optional parameters by using the params keyword. It allows you to pass any variable number of parameters to a method. But you can use the params keyword for only one parameter and that parameter is the last parameter of the method.
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
output
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With