Tables in your Gherkin Scenarios
When writing gherkin, we describe the expected behavior in
business terms. Sometimes, this is text;
other times, mixing small tables or lists helps to describe the behavior
better. In the following feature file,
there are examples of three types of gherkin tables:
Each of these tables has different implementation steps.
Each is simple to use, but it is easy to use the wrong method by mistake (this
happened to me)
Simple List of Items
In the first example, Check for pets, there is a simple list
of items. In the Ruby/Cucumber
implementation, you use the ‘raw’ method to handle the list of items. Use raw when you do not have a column header.
Table with Headers
In the second scenario, Check for valuables, there is a
table of items with a header. In this example, use the hashes method. Hashes
returns an array of hashes. With this
collection of hashes, you can then retrieve your values normally.
List of Items with Side Header
In the final scenario, Check denominations of money in safe,
there is an example of named list of items with the column on the left side.
With this type of table, you use the rows_hash method. This returns your data as a hash. In this
example, you can see label (which is a Ruby symbol) follows the format of
:dollar_bills_[denomination]. This is
because Ruby symbols cannot begin with numbers. This impacts the readability a
little but not too much.
Scenario outlines with examples are a way to data drive
scenarios using tables. I didn’t include
an example of them. They are generally
well understood and there are more examples of them than there are of the other
tables.