2017-01-30 30 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 

也就是說,And子句與表和Given條款沒有表調用/重複使用相同的功能,只能用一個表中的條款稱之爲多次。同時,其他子句只被調用一次 - 不是每行一次,我認爲這與使用場景上下文不同。

但這並不奏效。我得到以下錯誤:

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

我可以把它用一個工作表中的唯一途徑 - public void GivenIHaveEnteredIntoTheCalculator(Table table),但我想使用一個表,而無需重寫功能。

+0

使用[場景輪廓(https://github.com/cucumber/cucumber/wiki/Scenario-概述)進行第二次測試。在這種情況下,需要使用關鍵字「Scenario outline:」。 –

+0

@JeroenMostert謝謝你,我查看了你提供的鏈接,但我認爲我所尋找的內容稍有不同。我編輯了我的問題,它是否使事情更清楚? – sashoalm

+1

是的,但在這種情況下,您確實需要某種形式的第二個功能,無法繞過它。您無法直接在SpecFlow /小黃瓜中用簡寫表達多步操作,您需要調整語言。一個簡單的解決方法是更改​​第二種情況下的語法(「鑑於我已經順序地將''輸入到計算器中),第二種方法採用」表「並簡單地在循環中調用第一種方法。我可以說,讀者也可以更清楚地說明這一點。這種委託助手方法可能非常有用。 –

回答

2

從另一步調用一步。

首先,場景:

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); 
    } 
}