Removing Duplication with Transforms

Another issue we have with this step definition is that we have to convert the string captured by the regular expression into an integer. In fact, now that we’ve added an assertion, we’ve had to do it twice. As our test suite grows, we can imagine these calls to to_i littering our step definitions. Even these four characters count as duplication, so let’s stamp them out.

To do this, we’re going to learn about a new Cucumber method, called Transform.

Transforms work on captured arguments. Each transform is responsible for converting a certain captured string and turning it into something more meaningful. For example, we can use this transform to take a matched argument that contains a number and turn it ...

Get The Cucumber Book, 2nd Edition 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.