Skip to content
This repository was archived by the owner on Nov 16, 2018. It is now read-only.

Calling Steps from Step Definitions

aslakhellesoy 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 pracical 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

Calling with tables and strings

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:

# ruby
Given /^an expense report for (.*) with the following posts:$/ do |date, posts|
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 expese 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 expese 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

Clone this wiki locally