Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoiding repeating yourself in Gherkin

So I've been using specflow for a while now and something has been bugging me.

This is an example of a scenario we are currently using:

Scenario: Select a region via selection button
    When I select "Scotland" from Available Regions
    And I click on the "Select One" button
    Then Available Regions does not contain "Scotland"
    And Selected Regions contains "Scotland"

Is there a away to avoid saying "Scotland" on almost every line ? Or does it make it more readable and I should just stick with it ?

like image 666
Mark Broadhurst Avatar asked Sep 18 '12 15:09

Mark Broadhurst


1 Answers

Before I attempt to answer your question, can I suggest you take a minute to go and read Dan North's Who's domain is it anyway.

So firstly I'd like to get rid of the line that says And I click on the "Select One" button, because I think that should implicitly be part of When I select "Scotland" from Available regions.

Now you have

Scenario: Select a region 
    When I select "Scotland" from Available Regions
    Then Available Regions does not contain "Scotland"
    And Selected Regions contains "Scotland"

And you could write it as

Scenario: Select a region 
    When I select "Scotland" from Available Regions
    Then Available Regions does not contain the last selected region
    And Selected Regions contains the last selected region

Is there much of a difference? Well probably not.

What I've found as I've spent more time with cucumber, is that it helps to refactor your scenarios as much as you refactor the code behind them. In C#/SpecFlow we could implement

    Then Available Regions does not contain "Scotland"

with

    [Then("Available Regions does not contain (.*)")]
    public void ThenAvailableRegionsDoesNotContain(string region)
    {
        AvailableRegions.Contains(region).ShouldBeFalse();
    }

and

    Then Available Regions does not contain the last selected region

with

    [Then("Available Regions does not contain the last selected region")]
    public void ThenAvailableRegionsDoesNotContainLastSelectedRegion()
    {
        ThenAvailableRegionsDoesNotContain(LastSelectedRegion);
    }

It honestly is up to you. Which dialect you prefer

like image 146
AlSki Avatar answered Sep 28 '22 01:09

AlSki