sabotero
sabotero

Reputation: 4365

Specflow - Scenario outilne with data table

I had a series of scenarios that look like this:

Given these receipts:
| Amount | Date       | Company         |
| 1000   | 2016/10/25 | One Company     |
| 1200   | 2016/10/20 | Another Company |
| 1500   | 2016/10/13 | My Company      |
And delay is 15
When I calculate date of payment
Then Date of payment should be 20

Given these receipts:
| Amount | Date       | Company         |
| 1000   | 2016/10/25 | One Company     |
| 1200   | 2016/10/20 | Another Company |
| 1500   | 2016/10/13 | My Company      |
And delay is 30
When I calculate date of payment
Then Date of payment should be 15

Given these receipts:
| Amount | Date       | Company         |
| 1000   | 2016/10/25 | One Company     |
| 1200   | 2016/10/20 | Another Company |
| 1500   | 2016/10/13 | My Company      |
And delay is 45
When I calculate date of payment
Then Date of payment should be 10

So I learned about Scenario outline and I tried to make one for the above scenarios but had trouble putting in the receipts for obvious reasons:

Given these receipts: '<receipts>'
And delay is <delay>
When I calculate date of payment
Then Date of payment should be '<dateOfPayment>'

Examples:
| delay | dateOfPayment | receipts                               |
| 15    |    20         | | Amount | Date       | Company     |  |
                        | | 1000   | 2016/10/25 | one company |  |
                        | | ..............................    |  |

Given that I want the same collection of, in this case, receipts for all scenarios in my Feature how can I declare a table that will be passed at the place of '<receipts>' in the scenario outline

Maybe, should I proceed with a different approach?

---------------------------------- EDITED --------------------------------

Maybe something like this could work (but It is not implemented in Gherkin):

Given these receipts: '<receipts>'
And delay is <delay>
When I calculate date of payment
Then Date of payment should be '<dateOfPayment>'

Examples:
| delay | dateOfPayment | 
| 15    |    20         | 

Placeholder: '<receipts>'
| Amount | Date       | Company         |
| 1000   | 2016/10/25 | One Company     |
| 1200   | 2016/10/20 | Another Company |
| 1500   | 2016/10/13 | My Company      |

Upvotes: 1

Views: 9819

Answers (2)

sabotero
sabotero

Reputation: 4365

I had this silly idea: Given that the receipts does not change I could maybe provide to the Scenario Outline a Given with the actual table rather than a placeholder

And it worked:

Scenario Outline: payment
Given these receipts:
| Amount | Date       | Company         |
| 1000   | 2016/10/25 | One Company     |
| 1200   | 2016/10/20 | Another Company |
| 1500   | 2016/10/13 | My Company      |
And delay is '<delay>'
When I calculate date of payment
Then Date of payment should be '<dateOfPayment>'

Examples: 
| delay | dateOfPayment |
| 15    | 20            |
| 30    | 10            |
| 45    | 5             |

If you are, as I am, working in .Net with Specflow and c# specflow will generate this method for the Givenwith the table:

[Given(@"these receipts:")]
public void GivenTheseReceipts()
{            
    ScenarioContext.Current.Pending();
}

Don't worry, just add the table parameter, and the table will be passed as parameter as it does with a normal scenario:

[Given(@"these receipts:")]
public void GivenTheseReceipts(Table table)
{
    var receipts = table.CreateSet<Receipt>(); // you can even create a set given you have defined the Receipt class
}

public class Receipt
{
    public decimal Amount { get; set; }
    public DateTime Date { get; set; }
    public string Company { get; set; }
}

------------------------- EDITED -------------------------------------

It seems as it would have also worked with Background:

Background: 
Given these receipts:
| Amount | Date       | Company         |
| 1000   | 2016/10/25 | One Company     |
| 1200   | 2016/10/20 | Another Company |
| 1500   | 2016/10/13 | My Company      |

Scenario Outline: payment
And delay is '<delay>'
When I calculate date of payment
Then Date of payment should be '<dateOfPayment>'

Examples: 
| delay | dateOfPayment |
| 15    | 20            |
| 30    | 10            |
| 45    | 5             |

Now, it's only a matter of style.

Notice also that Background will be called before all other scenarios you may have in your Feature file.

Upvotes: 4

Seb Rose
Seb Rose

Reputation: 3666

Scenario Outline does not support table substitution.

You could, however, use:

| delay | dateOfPayment | rcpt1Amnt | rcpt1Date | rcpt1Cmpny | rcpt2Amnt | recpt2Date | ... | 15 | 20 | 1000 | 2016/10/25| One Company| 1200 | 2016/10/20 | ...

Upvotes: 1

Related Questions