Read more

Override Cucumber steps without an ambiguity error

Henning Koch
November 26, 2015Software engineer at makandra GmbH

Cucumber Show archive.org snapshot raises a Cucumber::Ambiguous if more than one step definitions match a step.

Illustration web development

Do you need DevOps-experts?

Your development team has a full backlog? No time for infrastructure architecture? Our DevOps team is ready to support you!

  • We build reliable cloud solutions with Infrastructure as code
  • We are experts in security, Linux and databases
  • We support your dev team to perform
Read more Show archive.org snapshot

Our new cucumber_priority Show archive.org snapshot gem provides a way to mark step definitions as overridable, meaning that they can always be overshadowed by a more specific version without raising an error.

This gem is currently used by spreewald Show archive.org snapshot and cucumber_factory Show archive.org snapshot .

Marking step definitions as overridable

To mark a step definition as overridable, call #overridable on the definition object:

Given /^there is a movie with a (.*?) tone$/ do
  ...
end.overridable

Given there is a movie with a funny tone do
  ...
end

The following step will now no longer raise Cucumber::Ambiguous:

Given there is a movie with a funny tone

If a step matches more than one non-overridable steps, Cucumber will still raise Cucumber::Ambiguous.

Defining priorities

You can define priorities for overridable steps by passing an numeric :priority option to #overridable:

Given /^there is a movie with a (.*?) tone$/ do
  ...
end.overridable(priority: 1)

Given /^there is a movie with a (sad|upbeat|disturbing) tone$/ do
  ...
end.overridable(priority: 5)

A higher priority wins the match.

A non-overridable step will always win over an overridable step regardless of its priority.

Posted by Henning Koch to makandra dev (2015-11-26 20:07)