I am following the technique outlined here
using a step defined like
[Given(\"some base scenario has happened\")]
public void SomeBaseScenarioHasHapp
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