【问题标题】:Inheritance in Specflow test steps cause Ambiguous StepsSpecflow 测试步骤中的继承导致不明确的步骤
【发布时间】:2017-09-18 21:04:23
【问题描述】:

我读过在使用 Specflow 时无法使用继承,这在大多数情况下是有意义的。但是,我遇到了一种似乎需要正确使用继承的情况。这是我的课程:

基类

public class StepBaseClass
{
    protected readonly ScenarioContext scenarioContext;

    public StepBaseClass(ScenarioContext scenarioContext)
    {
        this.scenarioContext = scenarioContext;
    }
}

第一个继承类:

    [Binding]
    public class StudioEnterpriseImportConnectorSteps:StepBaseClass
    {
        public StudioEnterpriseImportConnectorSteps(ScenarioContext scenarioContext) :base(scenarioContext)
        {

        }
        [Given(@"I have a data record that I want to send to the import service")]
        public void GivenIHaveADataRecordThatIWantToSendToTheImportService()
        {
            scenarioContext.Pending();
        }

        [When(@"I send the information to an invalid URL")]
        public void WhenISendTheInformationToAnInvalidURL()
        {
            scenarioContext.Pending();
        }

        [Then(@"an error should be generated")]
        public void ThenAnErrorShouldBeGenerated()
        {
            scenarioContext.Pending();
        }
    }

第二个继承类:

    [Binding]
    public class SitemapSteps:StepBaseClass
    {
        public SitemapSteps(ScenarioContext scenarioContext):base(scenarioContext)
        {
        }
        [When(@"I visit the URL (.*)")]
        public void WhenIVisitTheSitemapURL(string URL)
        {
            scenarioContext.Add("result", TestUtilities.GetResponseCode(URL));
            scenarioContext.Add("response", TestUtilities.GetResponseBody(URL));
        }

        [Then(@"the response code should be (.*)")]
        public void ThenTheResponseCodeShouldBe(string responseCode)
        {
            HttpStatusCode result = scenarioContext.Get<HttpStatusCode>("result");
            Assert.Equal(responseCode, result.ToString());
        }
    }

如您所见,我唯一继承了scenarioContext,这是为了编写多线程测试我需要做的事情。因此,我不想为我的每个类重复这段代码,而是希望能够从基类继承。初始化该变量以便我可以在每个派生类中使用它的正确方法是什么?

【问题讨论】:

  • 对您的措辞的小注解:您为每个绑定类编写此代码,而不是测试。绑定是全局的,您不必为每个功能/场景都重新编写它。
  • 谢谢@AndreasWillich。你是对的。我更新了我的语言。

标签: c# inheritance specflow


【解决方案1】:

正确的方法始终取决于您的个人情况。 我建议始终不要使用基类并在任何地方使用上下文注入。在构造函数中重复的少量代码对于很好地分离和拆分绑定及其实现来说是很小的代价。

为了获得关于这个主题的更多信息,Gaspar Nagy 写了一篇关于 SpecFlow 中步骤基类的优缺点的精彩博客文章: http://gasparnagy.com/2017/02/specflow-tips-baseclass-or-context-injection/

【讨论】:

    【解决方案2】:

    在 Specflow 测试挂钩中初始化我的依赖注入后,我将有一个名为 ApplicationContext 的类,它带有一个静态解析方法,它将返回我的 ScenarioContext 实例,如下所示:

    public class ApplicationContext
    {
        public static T Resolve<T>() where T: class 
        {
            return container.GetInstance<T>();
        }
    }
    

    然后在我的步骤类中,我会像这样解决ScenarioContext

    scenarioContext = (ScenarioContext)ApplicationContext.Resolve<IScenarioContext>();
    

    【讨论】:

      猜你喜欢
      • 2014-10-15
      • 2011-07-16
      • 2014-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多