I started a new home project the other day, a small website and started to write the features that I wanted, now I knew that SpecSalad supported tables in the usual Scenario Outline format as I had used them when writing the calculator example, but this time I wanted to include the table as part of the scenario for example
So simply expecting it to ‘Just Work’ I created my specification and watched it fail with some very interesting error messages! As it turned out there was actually no provision for the step definitions to take SpecFlow tables, so some time later I now have a couple of scenarios that can cope with tables both in the Given part of a scenario and the Then part, I didn’t add anything to the when syntax as I think currently this should be a unique call and probably won’t need tables.
So how does it work? using the above example first the given line above uses the new SpecSalad syntax
Given I can see a table Table Name
Given you can see a table Table Name
Given the Role Name can see a table Table Name
Then I should see Table Name table
Then you should see Table Name table
Then the Role Name should see Table Name table
For the Given steps unusually you don’t create a task as the framework simply saves the table into the current scenario context using the table name supplied as the key.
In the above scenario the rest of the steps are just the usual Spec Salad syntax, resulting the the following task code.
this calls the role to add a vote for the selected colour, the role in this case is TableSpecification
this simply loops through the table stored in the scenario context looking for the given colour and adds one to the vote for that colour, re-saving the table back to the scenario context.
The then step
this loops through the stored table looking for a row with the highest vote count storing the winning colour, this is then returned from the task to the framework to compare to the value in the specification.
I also wanted a syntax that could compare multiple results, so I could have a draw and two winning colours, the scenario looks like.
Most of the steps are the same, but now the then step also has a table with the expected results in, the There task looks like.
The then task first retrieves the table stored in the context, then goes through the table to find the top vote.
A new table is created that has the same shape as the table in the context and into that table each row that has the max score is added, it is then this new table that is returned to the framework to be compared to the expected results.
This last part is interesting as I am attempting to compare one table with another, not knowing if the two tables are even the same shape!
First up the framework asserts that the two tables have the same number of rows, after which the framework builds a list containing line contains the combined row values in the expected table.
The framework then iterates over the actual answers table and builds a string of the row values which is checked against the list form the expected values to ensure it exists.
I am not sure if this is the best way to achieve this, but currently it works!
This new syntax is available on Nuget as version 1.9.1