在条件下如何为多部分实现SpecFlow Hook?

时间:2011-12-13 19:55:04

标签: c# bdd specflow

我正在为我们的企业开发自动付款处理服务。我们正在尝试实现规范流程,并且当条件

时我们已成功完成了1
Scenario Outline: Stub matches NEXT oldest outstanding bill
    Given I have a group with the following bills:
        | Id     | Due Date   | Status               | Amount Due |
        | Bill A | 9/14/2011  | <Oldest Bill Status> | 1100.00    |
        | Bill B | 10/14/2011 | Unpaid               | 1200.00    |
        | Bill C | 11/14/2011 | Unpaid               | 1300.00    |
    When a stub is received that matches 'Bill B'
            And the Payment Amount is <Payment Amount>
    Then the allocation result should <Allocation Result>
            And Review should <Review>
Examples:
    | Oldest Bill Status | Allocation Result | Review       | Payment Amount |
    | Reconciled         | Allocate to Bill  | not required | 1200.00        |

[When(@"a stub is received that matches '(.*)'")]
        public void WhenAStubIsReceivedThatMatches(string billKeyName)
        {
           // Method
            }

我不确定在执行结果步骤之前如何更改钩子来处理这两个条件。

2 个答案:

答案 0 :(得分:2)

最佳做法是使用一个When语句。

一些选项包括:

 When a stub is received that matches 'Bill B' for <Payment Amount>

因此:

[When(@"a stub is received that matches '(.*)' for ([\d\.]*)")]
public void WhenAStubIsReceivedThatMatches(string billKeyName, decimal amount)
{
    // create stub and "recieve" it
}

OR

 Given ...etc...
 And I have a stub is received that matches 'Bill B' for <Payment Amount>
 When I receive the stub

...因为存根本身不是被测试的对象,它是背景数据,可以合理地创建为你的数量的一部分。

答案 1 :(得分:1)

有多个When-clause被废弃......我认为你可以理解(并解释)为什么,因为你提出这个问题。将几个动作放在一起很难,因为它们彼此依赖。

尝试使用简单的When子句编写规范,而是编写不同的场景来描述不同的交互方式。

因此,对于您的场景,如果您愿意,尝试找出发生的重要状态转换,以便从Given-state转到Then-state。

这是我发现用于编写场景的一个小方法

  • 首先写一个简短的标题
  • 然后开始填写Then-steps。你最终会得到什么?需要什么断言?
  • 然后编写When-steps。我如何进入Then-state
  • 然后编写给定步骤。在我可以执行When-clause之前需要什么上下文?
  • 现在修改标题,看看你是否已经提出了需要考虑的其他方案。

几个星期前我和Gjoko Adzic一起参加了一个课程,并且收到了很多这样的提示。 Read about it here