在步骤中调用步骤的Specflow会导致“无匹配步骤定义”错误

问题描述 投票:4回答:3

我遵循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调用基本场景?

specflow
3个回答
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绑定,没有这样的绑定 - 唯一的绑定是GivenWhenThenAndBut关键字仅在生成运行的单元测试时使用,使用这些关键字的步骤最终仍然绑定到GivenWhenThen步骤。

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

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

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

Given("Some other condition");

但你可以在一个场景中像这样引用它:

Given some condition
And some other condition

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


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()
{

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


0
投票

此问题之前已正确回答过。

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

我遇到这个问题的原因是,我忘了将属性[Binding,Scope(Feature =“My Feature”)]放在我的步骤类之上,这需要匹配顶部的“功能:我的功能”我的功能文件。

我刚刚教导我会在这里记录它以帮助其他人看到同样的错误,但出于我概述的不同原因。

© www.soinside.com 2019 - 2024. All rights reserved.