2017-09-18 3 views
1

ほとんどの場合、Specflowを使用すると継承は使用できません。しかし、私は適切な継承の使用を必要とするような状況に遭遇しました。ここに私のクラスである: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を継承している唯一のことは、マルチスレッドテストを書くために必要なことです。だから、私のクラスごとにこのコードを繰り返すのではなく、基底クラスから継承できるようにしたいと考えています。その派生クラスのそれぞれで使用できるように、その変数を初期化する適切な方法は何ですか? 、私のステップのクラスで次に

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

+0

小型のノートを自分の言葉遣いに:あなたはすべてのバインディング・クラスのために、このコードを書きます、テストではありません。バインディングはグローバルなので、すべての機能/シナリオで再度バインドする必要はありません。 –

+0

ありがとう@AndreasWillich。あなたは正しいです。私は自分の言語を更新しました。 –

答えて

0

Specflowテストフックに私の依存性注入を初期化した後、私はそうのように私に私ScenarioContextインスタンスを返す静的な解決方法でApplicationContextというクラスを持っているでしょう

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

適切な方法は、個々のsituaionにいつものように依存します。私はこのようなScenarioContextを解決するでしょう。 私はいつもベースクラスを使用せず、どこにでもコンテキストインジェクションを使うことを勧めます。コンストラクタで繰り返される少数のコードは、バインディングとその実装を適切に分離して分割するための小さな価格です。

このトピックに関する詳細情報を取得するには、ガスパルナジはSpecFlowのステップ基底クラスの長所と短所についての素晴らしいブログの記事を書いた: http://gasparnagy.com/2017/02/specflow-tips-baseclass-or-context-injection/

関連する問題