Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optional parameters with Specflow

Tags:

c#

bdd

specflow

How do I best handle the fact that many of my steps should take 0, 1 or even 2 dates as "first parameters"? Eg, how do I best make support for optional arguments in Specflow

The easiest case is when dates are of no concern as long as the steps happen after each other

 Given Peter was born 23-06-1973
 And Peter created an account
 And Peter deposited $200

Often though, a lot of steps that are time-dependent, such as

 Given Peter was born 23-06-1973
 And at 1-4-2012 Peter created an account
 And at 13-4-2012 Peter deposited $200

At other times there are two dates such as the real-time date and the date when something had happened. Eg. Peter filled out a printed form 14-4-2012 for a money transfer, but the form got lost for a few days, and we need to record today that the form was filled out a few days ago.

 Given Peter was born 23-06-1973
 ...
 And at 16-4-2012 really at 14-4-2012 Completed a transfer form to transfer $100 to account 12345 
like image 576
Carlo V. Dango Avatar asked Apr 23 '12 13:04

Carlo V. Dango


People also ask

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.

Does C# support optional parameters?

Use optional parameters in C#Optional parameters in the C# programming language allow you to specify arguments in a method signature that the caller of the method is free to omit. In other words, while you must specify values for required parameters, you might choose not to specify values for optional parameters.

What is the use of optional parameter?

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.


Video Answer


1 Answers

I prefer the simplicity of multiple steps, but if you want do do what you propose, you will need to craft an appropriate regex. Something like (not tested):

[Given(@"(at ([0-9-]+) (really at ([0-9-]+) |)|)(\w+) Completed a transfer form to transfer \$(\d+) to account (\d+)"]
public void TransferStep(string dummy1, DateTime? atDate, string dummy2, DateTime? reallyAtDate, string name, decimal amount, int account)
like image 147
mancaus Avatar answered Oct 12 '22 03:10

mancaus