Specflow - 带有数据的场景轮廓 table

Specflow - Scenario outilne with data table

我有一系列看起来像这样的场景:

Given these receipts:
| Amount | Date       | Company         |
| 1000   | 2016/10/25 | One Company     |
| 1200   | 2016/10/20 | Another Company |
| 1500   | 2016/10/13 | My Company      |
And delay is 15
When I calculate date of payment
Then Date of payment should be 20

Given these receipts:
| Amount | Date       | Company         |
| 1000   | 2016/10/25 | One Company     |
| 1200   | 2016/10/20 | Another Company |
| 1500   | 2016/10/13 | My Company      |
And delay is 30
When I calculate date of payment
Then Date of payment should be 15

Given these receipts:
| Amount | Date       | Company         |
| 1000   | 2016/10/25 | One Company     |
| 1200   | 2016/10/20 | Another Company |
| 1500   | 2016/10/13 | My Company      |
And delay is 45
When I calculate date of payment
Then Date of payment should be 10

所以我了解了 Scenario outline 并尝试为上述情况制作一张,但由于显而易见的原因无法放入收据:

Given these receipts: '<receipts>'
And delay is <delay>
When I calculate date of payment
Then Date of payment should be '<dateOfPayment>'

Examples:
| delay | dateOfPayment | receipts                               |
| 15    |    20         | | Amount | Date       | Company     |  |
                        | | 1000   | 2016/10/25 | one company |  |
                        | | ..............................    |  |

Given 我想要相同的集合,在这种情况下,receipts 用于我的 Feature 中的所有 scenarios 我如何声明一个 table 将在 scenario outline

中的 '<receipts>' 处传递

也许,我应该采用不同的方法吗?

-------------------------------- 已编辑 ---------- ----------------------

也许这样的事情可行(但它没有在 Gherkin 中实现):

Given these receipts: '<receipts>'
And delay is <delay>
When I calculate date of payment
Then Date of payment should be '<dateOfPayment>'

Examples:
| delay | dateOfPayment | 
| 15    |    20         | 

Placeholder: '<receipts>'
| Amount | Date       | Company         |
| 1000   | 2016/10/25 | One Company     |
| 1200   | 2016/10/20 | Another Company |
| 1500   | 2016/10/13 | My Company      |

Scenario Outline 不支持 table 替换。

但是,您可以使用:

| delay | dateOfPayment | rcpt1Amnt | rcpt1Date | rcpt1Cmpny | rcpt2Amnt | recpt2Date | ... | 15 | 20 | 1000 | 2016/10/25| One Company| 1200 | 2016/10/20 | ...

我有一个愚蠢的想法:鉴于收据没有改变,我也许可以向 Scenario Outline 提供一个 Given 实际 table 而不是 placeholder

成功了:

Scenario Outline: payment
Given these receipts:
| Amount | Date       | Company         |
| 1000   | 2016/10/25 | One Company     |
| 1200   | 2016/10/20 | Another Company |
| 1500   | 2016/10/13 | My Company      |
And delay is '<delay>'
When I calculate date of payment
Then Date of payment should be '<dateOfPayment>'

Examples: 
| delay | dateOfPayment |
| 15    | 20            |
| 30    | 10            |
| 45    | 5             |

如果您像我一样在 .Net 中使用 Specflowc# specflow 将为 Giventable:

[Given(@"these receipts:")]
public void GivenTheseReceipts()
{            
    ScenarioContext.Current.Pending();
}

不用担心,只需添加table参数,table就会像正常情况一样作为参数传递:

[Given(@"these receipts:")]
public void GivenTheseReceipts(Table table)
{
    var receipts = table.CreateSet<Receipt>(); // you can even create a set given you have defined the Receipt class
}

public class Receipt
{
    public decimal Amount { get; set; }
    public DateTime Date { get; set; }
    public string Company { get; set; }
}

------------------------ 已编辑 ------------------ ------------------

它似乎也适用于 Background:

Background: 
Given these receipts:
| Amount | Date       | Company         |
| 1000   | 2016/10/25 | One Company     |
| 1200   | 2016/10/20 | Another Company |
| 1500   | 2016/10/13 | My Company      |

Scenario Outline: payment
And delay is '<delay>'
When I calculate date of payment
Then Date of payment should be '<dateOfPayment>'

Examples: 
| delay | dateOfPayment |
| 15    | 20            |
| 30    | 10            |
| 45    | 5             |

现在,这只是风格问题。

另请注意,Background 将在您的 Feature 文件中的所有其他 scenarios 之前被调用。