Cucumber_tricks Gem : My Favorite Gherkin and Cucumber Tricks

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

1
2
Given the tool 'screwdriver'
When this tool is used

steps.rb

1
2
3
4
5
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

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

foo.feature

1
2
3
4
5
6
Given the dog "Rolphy"
...
Given the following dogs
  | Rex  |
  | King |
  | Volt |

Add default values to the hashes of a table

foo.feature

1
2
3
4
Given the following dogs
  | names | color |
  | Rex   | white |
  | King  | Sand  |

steps.rb

1
2
3
4
5
6
7
8
9
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

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

steps.rb

1
2
3
4
5
6
7
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.

Comments