Recipe 5Define Steps as Regular Ruby Methods

Problem

You’d like your step definitions to be plain Ruby methods so that they’re easier to edit, test, and maintain.

Ingredients

  • Cucumber’s built-in support for invoking Ruby methods directly[34]

  • (Optional) Mechanize[35] to run the examples with live data

Solution

Cucumber step definitions are pretty easy to put together. You just tie together a regular expression with a block of code. Ideally, these blocks of code should be really short—perhaps a method invocation or two and some data massaging.

Over time, it can be tempting to let more and more code creep into your step definitions. They can become harder to read and maintain.

Regular Ruby methods don’t have this problem. They’re easy to refactor ...

Get Cucumber Recipes now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.