Skip to content

Calling Steps from Step Definitions

Elufimov edited this page Sep 6, 2012 · 6 revisions

It is possible to call steps from Step Definitions:

[Binding]
public class CallingStepsFromStepDefinitionSteps : Steps
{
	[Given(@"the user (.*) exists")]
	public void GivenTheUserExists(string name)
	{
		// ...
	}

	[Given(@"I log in as (.*)")]
	public void GivenILogInAs(string name)
	{
		// ...
	}

	[Given(@"(.*) is logged in")]
	public void GivenIsLoggedIn(string name)
	{
		Given(string.Format("the user {0} exists", name));
		Given(string.Format("I log in as {0}", name));
	}
}	

Invoking steps from step definitions is practical if you have several common steps that you want to perform in several scenarios. -Or simply if you want to make your scenarios shorter and more declarative. 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 steps with multiline step arguments

Sometimes you want to call a step that has been designed to take Multiline Step Arguments, for example:

Tables

[Given(@"an expense report for (.*) with the following posts:")]
public void GivenAnExpenseReportForWithTheFollowingPosts(string date, Table postTable)
{
	// ...
}

This can easily be called from a plain text step like this:

# feature
Given an expense 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:

[Given(@"A simple expense report")]
public void GivenASimpleExpenseReport()
{
    string[] header = {"account" , "description", "amount"};
    string[] row1 = {"INT-100" , "Taxi", "114"};
    string[] row2 = {"CUC-101" , "Peeler", "22"};
    var t = new Table(header);
    t.AddRow(row1);
    t.AddRow(row2);
    Given("an expense report for Jan 2009 with the following posts:", t);
}

Clone this wiki locally