2016-11-12 85 views
0

步驟匹配可以通過範圍確定: https://github.com/techtalk/SpecFlow/wiki/Scoped-bindings。 但這是靜態機制。 有沒有一種方法,使這一機制的動態,以便:(?屬性)specflow的動態步驟定義匹配

  1. 在單一的情況下,我有兩個或更多相同的步驟
  2. 有這一步飾有很多的步驟定義
  3. 我有步驟來改變上下文和基於密鑰集在這一步不同的方法將被稱爲

這就像標籤限制從範圍機制+動態。

我測試應用程序與不同頁面上的多個表。我一定要驗證是否:

  • 表包含行(爲子集)
  • 表有確切行(項目和順序)(等於)
  • 表有行(但順序並不重要)。

我寫:

When I am on Page1 
Then I expect that table contains 
| Column1Name | Column2Name | Column3Name | 
| row1id    | true     | address   | 
| row2id    | true     | address   | 
When I do some change action on rows 
| Column1Name | 
| row1id    | 
Then I expect that table contains 
| Column1Name | Column2Name | 
| row1id    | false    | 
| row2id    | true     | 

我只有一個「我預計,表中包含」步驟定義。 它驗證步驟中提供的單元格。 此步驟用於不同頁面上的不同列和頁面。 這些頁面顯示不同的業務對象,有不同的列,但用戶界面相當普遍。 沒有這個,我將不得不爲每個頁面和每個列組合編寫步驟。

但是和往常一樣,在「我希望該表格包含」之前應該採取一些特定的操作。 例如:

  • 在不同的頁面上使用不同的搜索過濾器。即採取行動之前需要改變
  • 超時出現在UI(服務,高速緩存等)

我不想因爲它使該方案有過多的不必要的信息給它明確寫入。

+0

這是不可能的,這聽起來像是它可能令人困惑。你爲什麼想要這個?你的用例是什麼?可能有比您想要找到的更好的解決方案 –

+0

感謝您的回答。我同意你的看法,可能會令人困惑。我編輯了我的問題並添加了示例場景草稿和概念。我想避免在我的案例中寫入許多相同/相似的步驟定義。 – bwojdyla

回答

0

正如Sam Holder所寫的,這是不可能的,但我通過注入Step行爲來達到目的。 我將具有SpecFlow.Autofac.SpecFlowPlugin的行爲註冊爲命名服務 https://github.com/gasparnagy/SpecFlow.Autofac 以下是示例代碼。實現更復雜。這只是草案。

有步驟改變上下文鍵。與Behavior類上面提供的相同。

public interface ITableBehavior 
    { 
     void Search(Table table); 
    } 

    [ContextKey("Page1")] 
    public class Page1Behavior : ITableBehavior 
    { 
     public void Search(Table table) 
     { 
      throw new System.NotImplementedException(); 
     } 
    } 

    [ContextKey("Page2")] 
    public class Page2Behavior : ITableBehavior 
    { 
     public void Search(Table table) 
     { 
      throw new System.NotImplementedException(); 
     } 
    } 

    [Binding] 
    public class Step 
    { 
     private ITableBehavior TableBehavior { get { return BehaviorResolver.Resolve<ITableBehavior>(); } } 

     [Then("I expcet that table contains")] 
     public void TableContains(Table table) 
     { 
       TableBehavior.Search(table); 
       //table contains implementation 
     } 
    }