This repository was archived by the owner on Nov 16, 2018. It is now read-only.
forked from cucumber/cucumber-ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
Calling Steps from Step Definitions
robholland edited this page Aug 13, 2010
·
23 revisions
It is possible to call steps from Step Definitions:
# ruby
Given /^the user (.*) exists$/ do |name|
# ...
end
Given /^I log in as (.*)$/ do |name|
# ...
end
Given /^(.*) is logged in$/ do |name|
Given "the user #{name} exists"
Given "I log in as #{name}"
end
This is practical if you have several common steps that you want to perform in several scenarios. This allows you to do this in a Scenario:
# feature Scenario: View last incidents Given Linda is logged in # This will in fact invoke 2 step definitions When I go to the incident page
Instead of having a lot of repetition:
# feature Scenario: View last incidents Given the user Linda exists And I log in as Linda When I go to the incident page
This is only possible in 0.1.99 and higher
Sometimes you want to call a step that has been designed to take a table as input:
(See Using tabular data in features for details about tables).
# ruby Given /^an expense report for (.*) with the following posts:$/ do |date, posts_table| # The posts_table variable is an instance of Cucumber::Ast::Table end
This can easily be called from a plain text step like this:
# feature Given an expese report for Jan 2009 with the following posts: | account | description | amount | | INT-100 | Taxi | 114 | | CUC-101 | Peeler | 22 |
But what if you want to call this from a step definition? There are a couple of ways to do this:
# ruby
Given /A simple expense report/ do
Given "an expense report for Jan 2009 with the following posts:", table(%{
| account | description | amount |
| INT-100 | Taxi | 114 |
| CUC-101 | Peeler | 22 |
})
end
Or, if you prefer a more programmatic approach:
# ruby
Given /A simple expense report/ do
Given "an expense report for Jan 2009 with the following posts:", Cucumber::Ast::Table.new([
%w{ account description amount },
%w{ INT-100 Taxi 114 },
%w{ CUC-101 Peeler 22 }
])
end