我遵循这里概述的技术
使用定义的步骤
[Given("some base scenario has happened")]
public void SomeBaseScenarioHasHappened()
{
Given("some condition");
And("some action");
When("some result");
}
Run Code Online (Sandbox Code Playgroud)
从像这样的场景
Scenario: Some dependant scenario
Given some condition
And some base scenario has happened
When some other action
Then some other result
Run Code Online (Sandbox Code Playgroud)
然而这一步
When some other condition
Run Code Online (Sandbox Code Playgroud)
产生以下错误 - >找不到该步骤的匹配步骤定义.使用以下代码创建一个:
[When(@"some other condition")]
public void Whensome other condition()
{
ScenarioContext.Current.Pending();
}
Run Code Online (Sandbox Code Playgroud)
我可以通过让基本场景仅使用Given来解决问题
[Given("some base scenario has happened")]
public void SomeBaseScenarioHasHappened()
{
Given("some condition");
Given"some action");
Given("some result");
}
Run Code Online (Sandbox Code Playgroud)
但这不是我应该做的.我错过了什么吗?为什么不能使用AND调用基本场景?
在Specflow中,只有3种类型的步骤.Given,When和Then.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
Run Code Online (Sandbox Code Playgroud)
Specflow查找具有绑定的步骤:
Given("some base scenario has happened")
Given("some other condition")
When("some other action")
Then("some other result")
Run Code Online (Sandbox Code Playgroud)
注意没有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()
{
...
}
Run Code Online (Sandbox Code Playgroud)
你不能And在复合步骤中使用,因为没有步骤实际上与a绑定,And没有这样的绑定 - 唯一的绑定是Given,When或Then.的And和But生成的运行单元测试时的关键字仅用于,使用这些关键字的步骤仍结合到一个Given,When或Then步骤最终.
在一个场景定义的东西按顺序处理,你可以很容易地知道一个什么And一步实际上是基于它后出现的一步,所以当specflow产生它知道什么台阶式使用步骤绑定(无论是Given,When或Then).当您从另一个步骤中调用aa步时,您明确地调用其中一个步骤绑定,并且必须使用它绑定的绑定来调用它.所以如果它绑定了这样的Given绑定:
[Given("some other condition")]
public void SomeOtherCondition()
{
...
}
Run Code Online (Sandbox Code Playgroud)
然后你必须从代码中调用它:
Given("Some other condition");
Run Code Online (Sandbox Code Playgroud)
但你可以在一个场景中像这样引用它:
Given some condition
And some other condition
Run Code Online (Sandbox Code Playgroud)
因为specflow知道它何时生成And some other condition实际调用Given绑定步骤的单元测试
这个问题之前已经在上面正确回答过。
我刚刚遇到同样的错误“找不到一个或多个步骤的匹配步骤定义”。
我遇到这个问题的原因是我忘记将属性 [Binding, Scope(Feature = "My Feature")] 放在我的步骤 c# 代码类上方,该类将方法链接到功能文件,需要匹配“功能:我的功能”位于我的功能文件顶部。
我只是告诉我,我会在这里记录它,以帮助其他看到相同错误但出于我概述的不同原因的人。
| 归档时间: |
|
| 查看次数: |
8119 次 |
| 最近记录: |