在步骤中调用步骤的Specflow会导致"没有匹配的步骤定义"错误

时间:2014-08-04 06:18:52

标签: specflow

我正在遵循概述here

的技巧

使用像

这样定义的步骤
[Given("some base scenario has happened")]
public void SomeBaseScenarioHasHappened()
{
   Given("some condition");
   And("some action");
   When("some result");
}

来自

之类的场景
Scenario: Some dependant scenario
Given some condition 
And some base scenario has happened
When some other action
Then some other result

然而步骤

  When some other condition

产生以下错误      - >找不到该步骤的匹配步骤定义。使用以下代码创建一个:

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

我可以通过让基本场景仅使用Given

来解决这个问题
[Given("some base scenario has happened")]
public void SomeBaseScenarioHasHappened()
{
   Given("some condition");
   Given"some action");
   Given("some result");
}
然而,这不是我应该做的。 我错过了什么吗? 为什么不能使用AND?

调用基本场景

4 个答案:

答案 0 :(得分:6)

在Specflow中,只有3种类型的步骤。 GivenWhenThen。在方案说明中使用And的步骤时,SpecFlow将查看上一步骤类型,并假定您的And步骤属于同一类型。

所以当你写这个

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

Specflow查找具有绑定的步骤:

    Given("some base scenario has happened")
    Given("some other condition")
    When("some other action")
    Then("some other result")

请注意,没有And绑定?

因此,您的解决方案是确保在复合步骤中必须避免使用And,并且只使用原始步骤具有的相同绑定(如果它们具有多个绑定,则使用其中一个绑定)。您的最终解决方案应如下所示:

[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()
{
   ...
}

您不能在复合步骤中使用And,因为没有步骤实际上与And绑定,没有这样的绑定 - 唯一的绑定是Given,{ {1}}或WhenThenAnd关键字仅在生成运行的单元测试时使用,使用这些关键字的步骤仍绑定到ButGiven或{{1最终迈出了一步。

在场景定义中,事物按顺序处理,您可以根据之后显示的步骤轻松告诉When步骤实际是什么,因此当specflow生成步骤绑定时,它知道要使用的步骤类型(ThenAndGiven)。当您从另一个步骤中调用一个步骤时,您明确地调用其中一个步骤绑定,并且必须使用它绑定的绑定来调用它。所以,如果它与When这样的绑定绑定:

Then

然后你必须从代码中调用它:

Given

但你可以在一个场景中这样说:

[Given("some other condition")]
public void SomeOtherCondition()
{
   ...
}

因为specflow知道它何时生成单元测试,Given("Some other condition"); 实际上正在调用Given some condition And some other condition 绑定步骤

答案 1 :(得分:2)

可能的解决方案

使用Given而不是And

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

使用多个绑定标记步骤,例如

[Given(@"some other condition")]
[When(@"some other condition")]
public void Whensome other condition()
{

但这并不总是具有语义意义,所以只有在确实有意义时才使用它。

答案 2 :(得分:0)

此问题先前已在上面正确回答。

我刚遇到相同的错误“找不到一个或多个步骤的匹配步骤定义”。

出现此问题的原因是,我忘记将属性[Binding,Scope(Feature =“ My Feature”)]放在我的步骤类的上方,该属性与“ Feature:My Feature”相匹配。在我的功能文件顶部。

我刚刚教过我将在此处记录它,以帮助遇到相同错误但由于我概述的原因不同的其他人。

答案 3 :(得分:0)

尝试验证您的句子是否有空格。即:给定一些描述(空白) 因此,该方法将显示为:[Given(“ some description”)]

相关问题