Specflow calling steps within steps causes “No matching step definition” error

强颜欢笑 提交于 2019-12-04 03:49:20

问题


I am following the technique outlined here

using a step defined like

[Given("some base scenario has happened")]
public void SomeBaseScenarioHasHappened()
{
   Given("some condition");
   And("some action");
   When("some result");
}

from a scenario like

Scenario: Some dependant scenario
Given some condition 
And some base scenario has happened
When some other action
Then some other result

However the step

  When some other condition

produces the following error -> No matching step definition found for the step. Use the following code to create one:

[When(@"some other condition")]
public void Whensome other condition()
{
ScenarioContext.Current.Pending();
}

I can work around the problem by having the base scenario only use Given

[Given("some base scenario has happened")]
public void SomeBaseScenarioHasHappened()
{
   Given("some condition");
   Given"some action");
   Given("some result");
}

however this is not what I should have to do. Am I missing something? Why cant the base scenario be called using an AND ?


回答1:


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




回答2:


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.




回答3:


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 class, 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.




回答4:


try verifying if your sentence has empty spaces. i.e: Given some description(empty space) so, in the method will appear like: [Given("some description ")]



来源:https://stackoverflow.com/questions/25112766/specflow-calling-steps-within-steps-causes-no-matching-step-definition-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!