Specflow calling steps within steps causes "No matching step definition" error
Possible solutions
Use Given instead of And
Scenario: Some dependant scenario
Given some base scenario has happened
Given some other condition
When some other action
Then some other result
or
Tag the step with more than one binding, e.g.
[Given(@"some other condition")]
[When(@"some other condition")]
public void Whensome other condition()
{
but this won't always make semantic sense, so use this only when it really does make sense.
This question has been previously answered correctly above.
I just came across the same error "No matching step definition found for one or more steps".
The reason that I had this issue, was that I had forgotten to put the attribute [Binding, Scope(Feature = "My Feature")] just above my steps c# code class which links methods to feature file, which is needed to match "Feature: My Feature" at the top of my feature file.
I just taught that I would document it here to help someone else that was seeing the same error but for the different reason that I outlined.
In Specflow there are only 3 types of steps. Given
, When
and Then
. When you use a step with And
in your scenario description SpecFlow looks at the previous type of step and assumes that your And
step is of the same type.
So when you write this
Scenario: Some dependant scenario
Given some base scenario has happened
And some other condition
When some other action
Then some other result
Specflow looks for step which have bindings:
Given("some base scenario has happened")
Given("some other condition")
When("some other action")
Then("some other result")
Notice there is no And
binding?
So your solution is to to ensure that in your composite step you must avoid using And
and just use the same binding (or one of them if they have multiple) that the original step had. Your final solution should look something like this:
[Given("some condition")]
public void SomeCondition()
{
...
}
[When("some action")]
public void SomeAction()
{
...
}
[Then("some result")]
public void SomeResult()
{
...
}
[Given("some base scenario has happened")]
public void SomeBaseScenarioHasHappened()
{
Given("some condition");
When("some action");
Then("some result");
}
[Given("some other condition")]
public void SomeOtherCondition()
{
...
}
[When("some other action")]
public void SomeOtherAction()
{
...
}
[Then("some other result")]
public void SomeOtherResult()
{
...
}
You can't use And
in the composite steps because no steps are actually bound with an And
, there is no such binding - The only bindings are Given
, When
or Then
. The And
and But
keywords are only used when generating the unit tests that are run, the steps using those keywords are still bound to a Given
, When
or Then
step ultimately.
In a scenario definition the things are processed in order and you can easily tell what an And
step actually is based on the step it appears after, so when specflow generates the step bindings it knows what step type to use (either a Given
, When
or Then
). When you are calling a a step from within another step you are explicitly calling one of those step bindings and you have to call it with the binding that it is bound with. So if it was bound with a Given
binding like this:
[Given("some other condition")]
public void SomeOtherCondition()
{
...
}
then you have to call it like this from the code:
Given("Some other condition");
but you could refer to it like this in a scenario:
Given some condition
And some other condition
because specflow knows when it generates the unit test that the And some other condition
is actually calling a Given
bound step