2017-01-30 5 views
1

で同じ機能にテーブルと単一の引数の両方を渡す:私は電卓に番号を追加するステップ関数持たSpecFlow

private readonly List<int> _numbers = new List<int>(); 
... 
[Given(@"I have entered (.*) into the calculator")] 
public void GivenIHaveEnteredIntoTheCalculator(int p0) 
{ 
    _numbers.Add(p0); 
} 

を、私はこの構文を使用して機能ファイルからそれを呼び出すことができます。

Given I have entered 50 into the calculator 

しかし、私はまた、関数は、テーブルの行ごとに一度呼び出されるべき場合には、テーブルを使用して同じ関数を呼び出したい:

@mytag 
Scenario: Add multiple numbers 
    Given I have entered 15 into the calculator 
    Given I have entered <number> into the calculator 
     | number | 
     | 10  | 
     | 20  | 
     | 30  | 
    When I press add 
    Then the result should be 75 on the screen 
同等でなければなりません

@mytag 
Scenario: Add multiple numbers 
    Given I have entered 15 into the calculator 
    And I have entered 10 into the calculator 
    And I have entered 20 into the calculator 
    And I have entered 30 into the calculator 
    When I press add 
    Then the result should be 75 on the screen 

、テーブルコールなしでテーブルとGiven句とAnd句/同じ機能を再利用する。すなわち、テーブルを持つ唯一の句は、複数回、それを呼び出します。同時に、他の句は1回だけ呼び出されます - 行ごとに1回ではなく、シナリオコンテキストの使用とは違うと思います。

ただし、これは機能しません。 public void GivenIHaveEnteredIntoTheCalculator(Table table)、私は機能を書き換えることなく、テーブルを使用していた - 私はそれを動作させることができ

TechTalk.SpecFlow.BindingException : Parameter count mismatch! The binding method 'SpecFlowFeature1Steps.GivenIHaveEnteredIntoTheCalculator(Int32)' should have 2 parameters

唯一の方法は、テーブルを使用している:私は次のエラーを取得します。

+0

[シナリオ概要](https://github.com/cucumber/cucumber/wiki/Scenario-を使用しますアウトライン)。この場合、キーワード「シナリオ概要:」が必要です。 –

+0

@ JeroenMostertありがとうございました。私があなたが提供したリンクを調べましたが、私が探しているものはやや異なっていると思います。私は自分の質問を編集した、それは物事をより明確にするか? – sashoalm

+1

はい、その場合、実際には何らかの形の第2の関数が必要です。 SpecFlow/gherkinに直接記述して複数ステップ操作を表現することはできません。言語を微調整する必要があります。簡単な回避策は、2番目のケース(「計算機に「」を順番に入力したと仮定します)の文法を変更し、2番目のメソッドは 'Table'を取り、単にループ内の最初のメソッドを呼び出します。私は間違いなく読者のためにこれをより明確に呼ぶだろう。この種の委任ヘルパーメソッドは非常に便利です。 –

答えて

2

別のステップから1ステップを呼び出します。

まず、シナリオ:今すぐ

Scenario: Add multiple numbers 
    Given I have entered the following numbers into the calculator: 
     | number | 
     | 15  | 
     | 10  | 
     | 20  | 
     | 30  | 
    When I press add 
    Then the result should be 75 on the screen 

ステップ定義:

[Given("^I have entered the following numbers into the calculator:")] 
public void IHaveEnteredTheFollowingNumbersIntoTheCalculator(Table table) 
{ 
    var numbers = table.Rows.Select(row => int.Parse(row["number"])); 

    foreach (var number in numbers) 
    { 
     GivenIHaveEnteredIntoTheCalculator(number); 
    } 
} 
関連する問題