Flexibility

The readability of Cucumber features helps teams learn to use a ubiquitous language when talking about the system they’re building. This is a really important benefit, because that consistent use of terminology helps reduce misunderstandings and allow communication to flow more smoothly between everyone on the team.

So, we want to encourage our feature authors to be consistent about the nouns and verbs they use in the Cucumber features, because it helps make features that can be readily understood by anyone on the team. Equally, we also want feature authors to be able to express themselves as naturally as possible, which means they may often use slightly different phrasing to mean exactly the same thing. This is fine; in fact, it’s to be encouraged. Cucumber features are all about communicating with business users in their language, and it’s important that we don’t force them to sound like robots.

To keep the features readable and natural, it’s useful to develop the skill to make your step definitions flexible enough to match the different ways something might be expressed by a feature author. This isn’t as hard as you might think.

The Question Mark Modifier

When matching business-facing Gherkin text, you’ll often want to indicate that you don’t care about the odd character in your match, such as when a word could be singular or plural:

 Given I have 1 cucumber in my basket
 Given I have 256 cucumbers in my basket

Like the star and the plus, the question mark modifies the character that precedes it, specifying how many times it can be repeated. The question mark modifier means zero or one times; in other words, it makes the preceding character optional. In step definitions, it’s particularly useful for plurals:

 Given(​/I have (d+) cucumbers? in my basket/​) ​do​ |number|
 # TODO: code goes here
 end

By putting a question mark after the s in cucumbers, we’re saying that we don’t care whether the word is singular or plural. So, this step definition will match both of the previous steps.

You’ll find that the question mark comes in handy to knock off some rough edges from your step definitions and make them nice and flexible for your feature authors. Another useful technique is to use a noncapturing group.

Noncapturing Groups

Remember back in Alternation we showed you how you can list a set of possible values for part of a regular expression, separated by pipe. We can use this same technique to add flexibility to our step definitions, allowing feature authors to use slightly different ways to say the same thing. There’s one little change we’ll need to make, but we’ll get to that in a minute.

Take this extremely common step for a web application:

 When I visit the homepage

Suppose someone comes along and writes another step that looks like this:

 When I go to the homepage

Both of these steps have identical meaning to the reader, but unfortunately a step definition for the first one won’t match the second one without some modification. It would be helpful to have a step definition to recognize both phrases, because it really doesn’t matter whether you say visit or go to—they both mean the same thing. We can use an alternate to relax the step definition to accept this slightly different phrasing:

 When(​/I (?:visit|go to) the homepage/​) ​do
 # TODO: code goes here
 end

Notice that we’ve had to prefix the list of alternates with another bit of regular expression magic. The ?: at the start of the group marks it as noncapturing, meaning Cucumber won’t pass it as an argument to our block.

Anchors

You might have noticed that the step definition snippets that Cucumber prints for undefined steps start with a ^ and end with a $. Perhaps you’ve become so used to seeing them that you’ve stopped noticing them altogether. These two metacharacters are called anchors, because they’re used to tie down each end of the regular expression to the beginning and end of the string that they match on.

You don’t have to use them, and we deliberately left them out of the example up to this point because we wanted to wait until we’d explained what they do. If you omit one or both of them, you’ll find you end up with a much more flexible step definition—perhaps too flexible. As a silly example, suppose we add the ^ anchor to the beginning but omit the $ at the end of our bank account step definition:

 Given(​/^I have deposited $(d+) in my Account/​) ​do​ |amount|
 # TODO: code goes here
 end

This will allow a creative feature author to write something like this:

 Given I have deposited $100 in my Account from a check my Grandma gave to me

Generally, it’s best to keep your regular expressions as tight as you can so that there’s less chance of two step definitions clashing with each other. That’s why the snippets that Cucumber generates for undefined steps always include the anchors. Still, leaving off the anchors is a trick worth knowing about that can sometimes come in handy.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.136.22.179