【发布时间】: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