SpecFlow is a tool that allows the writing of business-readable tests that can then be automated in code. If you’re new to SpecFlow check out my Pluralsight course to get up to speed before looking at these more advanced topics.
Individual step definitions that represent the natural language scenarios are separate methods that get executed. Often we need to pass data between these steps. The simplest approaches to get started with are to use simple fields in the class containing the steps or to use the Scenario Context. An alternative to these approaches is to use Context Injection.
For example, imagine the following scenario:
Feature: SomeFeature
Scenario: Add New Contact Name
Given I have entered Sarah
When I choose add
Then the contact list should show the new contact
Using the underscore step definition format we would end up with step definitions something like this:
using TechTalk.SpecFlow;
namespace ContextInjectionExample
{
[Binding]
public class SomeFeatureSteps
{
[Given]
public void Given_I_have_entered_NEWNAME(string newName)
{
// etc.
}
[When]
public void When_I_choose_add()
{
// etc.
}
[Then]
public void Then_the_contact_list_should_show_the_new_contact()
{
// etc.
}
}
}
Whatever parameterised name is used in the Given step, we need to be able to use this in the Then step to assert that the contact list does in fact contain the new name.
To use Context Injection to do this, we first define a class to hold the new contact details:
public class NewContactDetails
{
public string Name { get; set; }
}
This class represent the “context” that we want to be able to share between step definition code.
The next step is to be able to access this context in all our steps in all our step definition files. To do this we create a constructor that has a parameter than represents our context, and store the object that SpecFlow auto-injects into this, in a local field in our steps class:
private readonly NewContactDetails _newContactDetails;
public SomeFeatureSteps(NewContactDetails newContactDetails)
{
_newContactDetails = newContactDetails;
}
Now we have our context, we can use it in our step definitions. We can set properties in the Given step:
[Given]
public void Given_I_have_entered_NEWNAME(string newName)
{
_newContactDetails.Name = newName;
}
And then read the property values in our Then step:
[Then]
public void Then_the_contact_list_should_show_the_new_contact()
{
var nameToCheckExists = _newContactDetails.Name;
// etc.
}
To learn more about Context Inject, check out my SpecFlow Tips and Tricks Pluralsight course.
You can start watching with a Pluralsight free trial.
SHARE: