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
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.
Deep Learning with Python by François Chollet(12590)
Hello! Python by Anthony Briggs(9926)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9802)
The Mikado Method by Ola Ellnestam Daniel Brolund(9787)
Dependency Injection in .NET by Mark Seemann(9348)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8309)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7772)
Grails in Action by Glen Smith Peter Ledbrook(7705)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7566)
Becoming a Dynamics 365 Finance and Supply Chain Solution Architect by Brent Dawson(7164)
Microservices with Go by Alexander Shuiskov(6933)
Practical Design Patterns for Java Developers by Miroslav Wengner(6850)
Test Automation Engineering Handbook by Manikandan Sambamurthy(6793)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6423)
Angular Projects - Third Edition by Aristeidis Bampakos(6209)
The Art of Crafting User Stories by The Art of Crafting User Stories(5722)
NetSuite for Consultants - Second Edition by Peter Ries(5656)
Demystifying Cryptography with OpenSSL 3.0 by Alexei Khlebnikov(5467)
Kotlin in Action by Dmitry Jemerov(5074)
