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 book lover

Growing Rails Applications in Practice

Check out our e-book. Learn to structure large Ruby on Rails codebases with the tools you already know and love.

  • Introduce design conventions for controllers and user-facing models
  • Create a system for growth
  • Build applications to last
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)