Override Cucumber steps without an ambiguity error

Posted . Visible to the public.

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

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.

Henning Koch
Last edit
Henning Koch
License
Source code in this card is licensed under the MIT License.
Posted by Henning Koch to makandra dev (2015-11-26 19:07)