Additional parameters with the Specflow function

What is the best way to handle the fact that many of my steps should take 0, 1, or even 2 dates as "first parameters"? For example, what is the best way to support additional arguments in Specflow

The simplest case is when the dates are not worried if the steps occur one after another

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

Often there are many time-dependent steps, for example

  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 happened. For instance. Peter filled out the printed form 14-4-2012 for a money transfer, but the form was lost for several days, and we need to write today that the form was completed 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 
+7
source share
3 answers

I prefer the simplicity of a few steps, but if you want to do what you suggest, you need to create a suitable regular expression. Something like (not verified):

 [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) 
+4
source

Two things appear in my head:

Firstly, now I’m not worried about the heavy load of step definitions if they are short (oneliners) and called at some level of automation or DSL, which helps me automate the system under test. Watch a great presentation for inspiration.

So, in this sense, you can simply double the definitions of the steps with "overloads" for each case.

My first blow to the second solution was to stack attributes on top of others for the same method. This works, but not for additional parameters. (And additional parameters do not work very well with reference types like DateTime, but does DateTime work? Nullable). If you activate your steps, you will receive an error message:

Parameter counter mismatch! The binding method 'Class1.MyGiven (Int32, Int32, Int32)' must have 1 parameter

So, I return to my first solution like this:

  [Given(@"case one with one int '(\d+)'")] public void Case1(int a) { // Short but sweet step definition that calls into automation layer } [Given(@"case one with two ints '(\d+)' and '(\d+)'")] public void Case2(int a, int b) { // Short but sweet step definition that calls into automation layer } [Given(@"case one with three ints '(\d+)' and '(\d+)' and also '(\d+)'")] public void Case3(int a, int b , int c) { // Short but sweet step definition that calls into automation layer } 

Hopefully I didn’t cause too much confusion going back and forth. Sorry - there was no IDE on the bus :)

+5
source

This worked well for me, overloading functions that require additional parameters, and passing default values.

It would be great if specflow supported optional parameters, for example string searchTerm = "" but at the moment it is not.

 [When(@"I request a list of managers")] public void WhenIRequestAListOfManagers() { WhenIRequestAListOfManagers(""); } [When(@"I request a list of managers with the search term (.*)")] public void WhenIRequestAListOfManagers(string searchTerm) { //do stuff } 
+2
source

Source: https://habr.com/ru/post/913932/


All Articles