What is the best way to reuse SpecFlow Given/When/Then steps? I figured out three ways with all specific advantages and drawbacks, but I am not convinced this is the best way.
I have two projects in one solution
ProjectA:
[Binding]
public class BookSteps : StepsBase
{
[Given(@"the following books:")]
public void GivenTheFollowingBooks(Table table)
{
// ...
}
}
- I can just inherit my steps like this:
ProjectB:
[Binding]
public class BookStepsReference : ProjectA.BookSteps { }
This works and requires the least work. Unfortunately it breaks the intellisense of the feature files: The steps stay purple in the ProjectB feature files.
- I can inherit and create a method with the same signature:
ProjectB:
[Binding]
public class BookStepsReference : ProjectA.BookSteps
{
[Given(@"the following books:")]
public void GivenTheFollowingBooksReference(Table table)
{
base.GivenTheFollowingBooks(table);
}
}
This breaks when I try to run the test because the auto-generated feature steps sees two methods with a Given attribute "the following books:" and throws an ambiguous reference exception.
- I make a private object that references the binding steps from the project A:
ProjectB:
[Binding]
public class BookStepsReference
{
private ProjectA.BookSteps _bookSteps = new ProjectA.BookSteps();
[Given(@"the following books:")]
public void GivenTheFollowingBooks(Table table)
{
_bookSteps.GivenTheFollowingBooks(table);
}
}
This works and also applies the correct intellisense on the Feature File steps. But when I want to debug my steps, I get an external COM exception on the initializing of the _baseSteps object, probably caused by the SpecFlow library that sees a double Binding attribute.
The last option is the way I am working right now, but I'd like to know if others have created a better way to reuse steps from other projects.