Rails 5 Test Prescriptions by Noel Rappin

Rails 5 Test Prescriptions by Noel Rappin

Author:Noel Rappin
Language: eng
Format: epub
Tags: Pragmatic Bookshelf
Publisher: Pragmatic Bookshelf


Advanced Cucumber

This section could be titled “Things Cucumber lets you do that are probably bad ideas.” Cucumber allows for a lot of flexibility in how steps match with step definitions. By and large, the Cucumber-using community has come to the conclusion that most of these things should be used sparingly, if at all.

Earlier I alluded to the idea that step definitions were regular expressions and not strings. This allows you to have the same step definition apply to multiple strings. More to the point, you can use regular-expression groups to capture those matches. The parts of the string that are in groups are then passed as block variables to the block part of the step definition. This allows you to treat a Cucumber step as a method call with parameters.

In the existing initial step, the project name is hard-coded inside the step definition. If, on the other hand, you wanted to be able to specify the name of the project in the Cucumber feature, you could write the step definition as follows:

​ Given ​/^a project named "(.*)"$/​ ​do​ |project_name|

​ @project = Project.create!(​name: ​project_name)

​ ​end​

That definition would match steps like this:

​ Given a project named ​"Rails 5 Test Prescriptions"​

​ Given a project named ​"Evil Master Plan"​

In each case the step definition would create a project with the given name.

You can go even further in terms of putting data in the Cucumber feature file. Cucumber allows you to append a table of data to a step. It’s a very clever feature, and like a lot of very clever features it should be used judiciously.

You use something like Markdown table syntax to create the table. In the feature file it might look something like this:

​ Given the following ​users:​

​ ​ ​| login| email | password| password_confirmation|

​ | alpha| [email protected]| alpha1 | alpha1 |

​ | beta | [email protected] | beta12 | beta12 |

The step with the table needs to end with a colon. The table uses pipe characters to delimit entries. They don’t have to line up, but usually you’ll want them to.

When Cucumber matches a step definition to a step that has a table, the table becomes an argument to the step definition’s block—if there are other regular-expression matches, the table is the last argument. The argument is a special Cucumber data type, and there are a few different ways you can deal with it. Most commonly you’ll deal with it as an array of hashes, where the keys are the first row of the table and every subsequent row provides a set of values, like so:

​ Given ​/^the following users$/​ ​do​ |user_data|

​ User.create!(user_data.hashes)

​ ​end​

You can do something similar with a large string literal:

​ Given I have typed the following

​ ​"""​

​ ​ some big amount of text​

​ ​ """​

That’s an indented line, three quotation marks, some text, and then three more quotation marks at the end. The text inside the triple quotes will be passed as the last argument to the step definition’s block.

If you really want to have fun, you can combine



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.