Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optional parameter in cucumber

Tags:

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]"|   
like image 857
larryq Avatar asked Aug 20 '13 23:08

larryq


People also ask

What is the use of optional parameter?

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.

How do you specify an optional parameter?

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.

What are optional parameters are added?

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.

Can you make a parameter optional?

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.


1 Answers

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 
like image 91
basti1302 Avatar answered Sep 21 '22 15:09

basti1302