Rails 5 Test Prescriptions: Build a Healthy Codebase by Noel Rappin

Rails 5 Test Prescriptions: Build a Healthy Codebase by Noel Rappin

Author:Noel Rappin [Rappin, Noel]
Language: eng
Format: epub, azw3, pdf
Tags: Pragmatic Bookshelf
Publisher: Pragmatic Bookshelf
Published: 2018-02-15T23:00:00+00:00


Writing Cucumber Steps

Sadly, it’s unrealistic for Cucumber to know what to do just from a step like Given a project. So you must define all the steps so Cucumber can execute them.

When Cucumber gets a step like Given a project, it searches through all the files in the step definition folder looking for one definition that matches. What does matching mean? Let’s look at the boilerplate for that step again:

Given(/^a project$/) do

pending

end

The first line of the definition is one of those Given/When/Then words (it doesn’t matter which one) followed by a regular expression. Cucumber matches a step to a definition when the sentence part of the step, in this case a project, matches the regular expression—such as /^a project$/. You’ll see later why Cucumber uses regular expressions instead of just strings. When Cucumber sees the step Given a project, it finds the step definition whose regular expression matches, then runs the code inside the block for the matching step definition. If Cucumber finds more than one matching step definition raises an error.

Inside a step definition you can write any arbitrary Ruby code. Instance variables declared in one step definition will be available to later step definitions in the same test. Be careful with instance variables; it’s not always easy to tell what variables might exist from previous steps, or what state they might be in. Cucumber understands Capybara methods and it understands RSpec matchers (assuming RSpec is installed). Arbitrary methods defined in any step-definition file will be available to any step definition.

Your case here is unusual in that you already have this feature managed as a non-Cucumber integration test, so filling the steps is mostly a question of splitting that test into pieces. My copy looks like this:

integration/03/features/step_definitions/add_task_steps.rb

Given(/^a project$/) do

@project = Project.create(name: "Bluebook")

@project.tasks.create(title: "Hunt the aliens", size: 1, project_order: 1)

@project.tasks.create(title: "Write a book", size: 1, project_order: 2)

end



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.