-
Notifications
You must be signed in to change notification settings - Fork 20
Steps
To link your specification in a textfile to code, you add the attribute Nbehave.Narrator.ActionStepsAttribute on the class where you intend to implement your steps. Each step in the text file need to be mapped to a method in a class with the ActionSteps attribute, this is done with the attributes Given, When and Then. The three attributes works the same way. The easiest way is to just add the attribute to a method and name the method as the actionstep in the text file with spaces replaced with underscores, ex: In the textfile you may have Given I have a Greeting system
This is easily mapped with the following code
[Given]
public void Given_I_have_a_greeting_system()
{
// code
}
or you can do it this way
[Given("I have a greeting system")]
public void GreetingSystem()
{
// code
}
If you need parameters you use the name of the parameter in the method name like this: Given my name is Morgan
[Given("my name is $userName")]
public void UserName(string userName)
{
// code
}
The $userName
in the string is mapped to the input parameter userName so that you can write
Given my name is Morgan
nbehave will call the UserName method with Morgan as the value of the parameter.
There's also the option to write your step like this
[Given]
public void Given_my_name_is_userName(string userName)
{
// code
}
In this case the method name will be used as step text. This is equivalent to the previous example. The function name will be turned into a [Given("my name is $userName")]. The parameter names will be match with the first occurrance in the method name using this method.
If you need more control over the parameters sent to a method you can specify a regular expression to map out parameters from the textfile to a method. Ex: ”Given my name is Morgan Persson” And you want to map ”Morgan Persson” to one parameter, this cannot be done with only [Given] so you have to specify a regular expression:
[Given(@”my name is (?<userName>\w+\s\w+)$”)]
public void Given_my_name_is(string userName)
{
// code
}
Its important that you match the parameter names in the function with the group names in your regular expression or Nbehave wont be able to match things together. Alias
Its possible to have multiple attributes on the same method so that your scenarios can be worded differently.
[Given("my name is $userName")]
[Given("a user named $userName")]
public void UserName(string userName)
{
// code
}
If you need a parameter to a step that is an array (or list) you can implement the step like.
[Given("a list of numbers $values")]
public void Given_array(int[] values)
{
// code
}
or
[Given("a list of numbers $values")]
public void Given_array(List<int> values)
{
// code
}
Both will match: Given a list of numbers 1, 2, 3, 5, 8