gpt4 book ai didi

c# - Specflow测试步骤继承导致 "Ambiguous step definitions"

转载 作者:可可西里 更新时间:2023-11-01 08:15:56 24 4
gpt4 key购买 nike

我想要具有以下测试步骤类结构:

[Binding]
public class BaseStep
{
[Given(@"there is a customer")]
public void GivenThereIsACustomer(Table table)
{
HandleCustomer(table);
}

protected virtual void HandleCustomer(Table table)
{
}
}

[Binding]
public class FeatureOneStep : BaseStep
{
protected override void HandleCustomer(Table table)
{
// feature one action
}

[Given(@"feature one specific step")]
public void GivenFeatureOneSpecificAction(Table table)
{
// do something
}

}

[Binding]
public class FeatureTwoStep : BaseStep
{
protected override void HandleCustomer(Table table)
{
// feature two action
}

[Given(@"feature two specific step")]
public void GivenFeatureTwoSpecificAction(Table table)
{
// do something
}
}

“假设有客户”是FeatureOne 和FeatureTwo 中共同使用的步骤,但在这两个特征内部会有不同的处理逻辑。所以我决定把这个步骤定义放到一个基类中,分别重写两个派生类中的protected方法。

但是,当我运行测试时,出现以下错误:

TechTalk.SpecFlow.BindingException: Ambiguous step definitions found for step
'Given there is a customer':
CustomerTestBase.GivenThereIsACustomer(Table),
CustomerTestBase.GivenThereIsACustomer(Table)

谁能告诉我如何解决这个问题?

最佳答案

我自己现在正在弄清楚这个问题,所以有几点说明(希望将来有人可以使用它):

  • 不要在基类上包含 [Binding] 属性
  • 为每个特征文件创建一个派生类
    • 将[Binding]属性添加到派生类(将自动包含基类中的所有步骤定义)
    • 为派生类添加一个[Scope]属性;为命名参数Feature指定特征的名称

关于c# - Specflow测试步骤继承导致 "Ambiguous step definitions",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25433031/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com