Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Providing multiple When statements for SpecFlow Scenario

Fairly new to SpecFlow so bear with me.

I was working with a colleague to get a basic understanding of what you can do with SpecFlow.

We were using the classic FizzBuzz problem, which we have used to test unit testing to compare how we would do a similar problem in SpecFlow.

we wrote our scenarios as follows growing the code as needed:

(please excuse naming just wanted to get the tests writ)

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 lead to an evolution to develop a method that would calculate a sum of some numbers

The scenario 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 accepted one number at a time to then sum up.

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 go about setting up the statement so that you can expect a params int[] on the method signature.

like image 703
Luke Duddridge Avatar asked Nov 05 '13 10:11

Luke Duddridge


People also ask

How do you run the same scenario multiple times in SpecFlow?

You can set up multiple test targets in your SpecFlow runner profile, and it will run the tests once for each defined target. This allows us to automatically run the same test suite multiple times, with different set-up values.

Can we have multiple When in BDD?

Another aspect to consider is that allowing multiple When-Then pairs per scenario indicates that a team sees more value in BDD's test framework than in its collaborative spec process.

What SpecFlow keywords allow generating multiple tests?

The Scenario Outline keyword can be used to run the same Scenario multiple times, with different combinations of values.

Can we use when after then in BDD?

No, if you have more than one When or Then - you have a separate use-case/example/test.


1 Answers

Your problem is supported really nicely by the specflow step bindings, if you use a StepArgumentTransformation. This is why I love specflow.

[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();
}

The StepArgumentTransformation here will allow you to take any comma separated list of ints in any step definition from now on, and accept it as an Array parameter.

It's worth learning a few regex bits if you want to play with StepArgumentTransformations, to make them nice and specific. Note I could have used (\d+(?:,\d+)*) instead of .* in the When binding too.

like image 81
perfectionist Avatar answered Nov 13 '22 12:11

perfectionist