How do you usually describe and implement 'Given' step for scenario?
- High level state description OR explicit data definitions?
- Fill database or stub repository?
High level state description
Given I have 4 products
When I look for best-selling products
Then I see top 3 products with maximum number of sales
PRO
- Not brittle
- Easy to read and understand business goal
CON
- Do not clear what data we need
Explicit data definition
Given I have following products:
| Name | Sales number |
| Beer | 20 |
| Pizza | 5 |
| Socks | 3 |
| Toilet paper | 100 |
When I look for best-selling products
Then I see following products:
| Name | Sales number |
| Toilet paper | 100 |
| Beer | 20 |
| Pizza | 5 |
PRO
- Easy to implement (clear what data needed)
CON
- Hard to read and understand business goal
- More brittle
Fill database
using (var connection = new SqlConnection(connectionString))
{
using (var deleteCommand = new SqlCommand("DELETE FROM Products", connection))
{
connection.Open();
deleteCommand.ExecuteNonQuery();
}
SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM Products", connection);
DataSet data = new DataSet();
adapter.Fill(data);
foreach (var specFlowRow in table.Rows)
{
DataRow dataRow = data.Tables[0].Rows.Add();
dataRow["Name"] = specFlowRow["Name"];
}
adapter.Update(data);
}
PRO
- Specs as integration tests (we exercise system end-2-end)
CONS
- We need to create database tables prior to code (data-driven approach)
- Slow
- Brittle
- Hard to implement
Repository stub
// or get stubbed repository from DI framework
productsRepository = new InMemoryProductsRepository();
// or use specflow assist helpers
foreach (var specFlowRow in table.Rows)
productsRepository.Save(new Product(specFlowRow["Name"]));
PRO
- We can do code first
- Fast
- Less brittle (easy to change)
- Easy to implement
CONS
- We do not have proofs that feature is implemented
Thats my vision of possible ways :) What way do you define and implement 'Given' steps? Thanks!