2011-05-31 124 views
1

這似乎是的,如果你知道答案,很明顯這些問題之一,如果你不這樣做,這是不可能的......多個多行示例

我如何包括SpecFlow特徵文件中的多行示例表格?

我的例子是:提前

 
     Given there is some invalid input: 
     | input | 
     | """ Multi-line example 1 
       because there are multiple lines 
       """ | 
     | """Multi-line example 2 
      with even more lines 
      than the previous example 
      """" | 
    When something interesting happens 
    Then the error is shown 

感謝。

回答

3

,你可以做這樣的:

Given there is some invalid input: 
    | <Here goes column Name> | <Column Name2..> | 
    | Line 1 for column 1  | Line 1 for column2| 
    | Line 2 for column 1  | Line 2 for column2| 
    | ..and so on    | and so on...  | 
When something interesting happens 
Then the error is shown 

,這將轉化爲

[Given(@"there is some invalid input:")] 
public void GivenThereIsSomeInvalidInput(Table table) 
{ 
    foreach (var row in table.Rows) 
    { 
     string info1= = row["<Here goes column Name>"]; 
     string info2= = row["<Column Name2..>"]; 
    } 
} 

,我知道你有一對夫婦的套無效輸入的,那麼你可以讓另一場景,就像這樣,只需要在表中添加更多的輸入數據,不需要額外的代碼。

希望這能解決你的問題

+0

這是一個很好的想法,我理解你的意思,但是這種做法失敗了無效輸入的單一場景的目的。如果我添加另一個場景,則需要多個多行參數消失,並且我可以使用常規的多行方法。 – grefly 2011-06-03 17:29:48

2

,因爲我的實際比較和預期值自己(不使用SpecFlow的自動錶比較功能),我允許使用特殊值的正則表達式,如含有換行符字符串:

Then I expect the result values 
    | Name   | Value    | 
    | Multilinestring | @@Multline\nString | 

和我比較功能是這樣的:

private static bool compare (string actual, string expected) 
{ 
    if (expected.StartsWith("@@")) 
     return Regex.Match(actual, expected.Substring(2)).Success; 
    .... 
}