当你编写Given When Then specs时,你更喜欢在1 When语句中列出一个元素中的元素,还是为每个元素做一个单独的规范?
即。
GIVEN a book WHEN category is "Fanatsy", "Sci-Fi", or "Horror" THEN Buy it.
VS。
GIVEN a book WHEN category is "Fanatsy" THEN Buy it.
GIVEN a book WHEN category is "Sci-Fi" THEN Buy it.
GIVEN a book WHEN category is "Horror" THEN Buy it.
此外,你如何处理其余的一组?您是否为每个不在Buy-On-Sight子集中的类别编写规范以确保它不会购买?
答案 0 :(得分:1)
最好是将它们分开,因为这样可以更容易地确定哪一个失败了。例如,如果您的测试未能购买“Sci-Fi”但是在“Fantasy”和“Horror”上取得成功,您怎么知道哪一个失败?困难的部分是哄骗你的语言让你这样做而不必一遍又一遍地重复自己。
这对于一维问题并不像多维问题那样成问题,例如:
GIVEN a bookstore
THAT has "Fantasy", "Sci-Fi", and "Horror" books
WHEN you only have enough money to buy one book
THEN buy "Fantasy"
和
GIVEN a bookstore
THAT has "Fantasy", "Sci-Fi", and "Horror" books
WHEN you have enough money to buy two books
THEN buy "Fantasy" and "Sci-Fi"
和
GIVEN a bookstore
THAT has "Fantasy", "Sci-Fi", and "Horror" books
WHEN you have enough money to buy two books
AND you already own a "Fantasy" book
THEN buy "Sci-Fi" and "Horror"
我在这些情况下的投票仍然是你将其分解:
GIVEN a bookstore
THAT has "Fantasy", "Sci-Fi", and "Horror" books
WHEN you have enough money to buy two books
AND you already own a "Fantasy" book
THEN buy "Sci-Fi"
THEN buy "Horror"
意味着您在上述情况下有两个规格。但是在静态语言中,比如C#和Java,我还没有找到一个很好的方法来做到这一点。有一点是有帮助的,就是分解你的安排并将代码断言为可重复使用的方法,并从他们那里挑选,就像你从自助餐厅的菜肴中挑选一样:
public virtual void Establish_context()
{
Given_a_bookstore_that_has_Fantasy_Sci_fi_and_Horror_books();
When_you_have_enough_money_to_buy_two_books();
And_you_already_own_a_fantasy_book();
}
[Specification]
public void Then_buy_Sci_fi()
{
}
[Specification]
public void Then_buy_Horror()
{
}
现在,您可以创建多个子类,混合并匹配您的Given和When。它减少了痛苦,但在编写非常流畅的BDD样式代码时仍然突出了静态类型语言的缺点。