2016-11-12 87 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 
     } 
    }