Providing multiple When statements for SpecFlow script

Slightly new to SpecFlow, so bear with me.

I worked with a colleague to get a general idea of ​​what you can do with SpecFlow.

We used the classic FizzBuzz problem that we used to test unit testing to compare how we would do a similar problem in SpecFlow.

we wrote our scripts as follows, increasing the code as needed:

(please excuse the naming just wanted a test report)

Scenario: 1 is 1 Given there is a FizzBuzzFactory When you ask What I Am with the value of 1 Then the answer should be 1 on the screen Scenario: 3 is Fizz Given there is a FizzBuzzFactory When you ask What I Am with the value of 3 Then the answer should be Fizz on the screen Scenario: 5 is Buzz Given there is a FizzBuzzFactory When you ask What I Am with the value of 5 Then the answer should be Buzz on the screen Scenario: 15 is FizzBuzz Given there is a FizzBuzzFactory When you ask What I Am with the value of 15 Then the answer should be FizzBuzz on the screen 

This leads to the evolution of the development of a method that would calculate the sum of some numbers

The script we wrote was:

 Scenario: Sumof 1 + 2 + 3 is Fizz Given there is a FizzBuzzFactory When you add the sum of 1 When you add the sum of 2 When you add the sum of 3 Then the answer should be Fizz on the screen 

The method we wrote took one number at a time, so that we could summarize.

Ideally, I would provide:

 Scenario: Sumof 1 + 2 + 3 in one go is Fizz Given there is a FizzBuzzFactory When you add the sum of 1,2,3 Then the answer should be Fizz on the screen 

How can you customize this statement so that you can expect params int[] in the method signature.

+6
source share
1 answer

Your problem is very well supported by flow step bindings if you use StepArgumentTransformation . That is why I like to vague.

 [When(@"you add the sum of (.*)")] public void WhenYouAddTheSumOf(int[] p1) { ScenarioContext.Current.Pending(); } [StepArgumentTransformation(@"(\d+(?:,\d+)*)")] public int[] IntArray(string intCsv) { return intCsv.Split(',').Select(int.Parse).ToArray(); } 

In this case, StepArgumentTransformation will allow you to select any of the commas-separated list of ints in any step definition from now on and accept it as an Array parameter.

It's worth exploring a few regular expressions if you want to play with StepArgumentTransformations to make them nice and specific. Note. I could use (\d+(?:,\d+)*) instead of .* In the When binding.

+9
source

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


All Articles