Cucumber_tricks gem : my favorite Gherkin and Cucumber tricks

1 minute read

I just compiled my Gherkin and Cucumber goodies into a gem. It’s called cucumber_tricks and the source code can be found on github. It’s also tested on travis and documented in details on relish.

The goal of all these tricks is to be able to write more natural english scenarios. Here is an extract from the readme of the gem, which explains what it can do :

Use pronouns to reference previously introduced items

foo.feature

Given the tool 'screwdriver'
When this tool is used

steps.rb

A_TOOL = NameOrPronounTransform('tool', 'hammer')

Given /^(#{A_TOOL})$/ do |tool|
  ...
end

Use the same step implementation to handle an inline arg as a 1-cell table

steps.rb

GivenEither /^the dog named "(.*)"$)$/,
            /^the following dogs$/ do |dogs_table|
  ...
end

foo.feature

Given the dog "Rolphy"
...
Given the following dogs
  | Rex  |
  | King |
  | Volt |

Add default values to the hashes of a table

foo.feature

Given the following dogs
  | names | color |
  | Rex   | white |
  | King  | Sand  |

steps.rb

Given /^the following dogs$$/ do |dogs|
  hashes = dogs.hashes_with_defaults('names', 'tail' => 'wagging', 'smell' => 'not nice')

#  hashes.each do |hash|
#    expect(hash['smell']).to eq('not nice')
#  end

  ...
end

Define named lists from a table

foo.feature

Given the following dishes
  | Spaghetti Bolognaise | => | Spaghetti | Bolognaise sauce |       |         |
  | Burger               | => | Bread     | Meat             | Salad | Ketchup |

steps.rb

Given /^the following dishes$$/ do |dishes|
  name_2_dishes = dishes.hash_2_lists

#  expect(name_2_dishes['Burger']).to eq(['Bread','Meat','Salad','Ketchup'])

  ...
end

Visit relish for more detailed documentation.

I usually write about 15 minutes worth of reading per month. I won't transfer your email. No Spam, unsubscribe whenever you want.

As a gift for subscribing, you'll receive an illustrated mini-ebook "How to start a team coding dojo"!

Leave a comment