ber*_*kes 6 bdd cucumber gherkin
假设我正在使用Cucumber开发购物车BDD.购物车相当复杂,有许多铃声和口哨声,但这对于"博客"或"用户档案"来说也是如此.
我一直认为"推车"是特色,而钟声和口哨是作为情景的.但是,这可能会生成大型Feature文件,并且违背了Scenario的字面含义.这是看起来如何:
Feature: Cart
So that I can buy items
As a user
I want to place items in a cart
#.... Many more scenarios
Scenario: Empty a filled cart
Given 2 products in my cart
When I visit the cart page
And I press "Empty cart"
Then I should see the text:
"""
Your cart is empty.
"""
Scenario: Empty an empty cart
Given 0 products in my cart
When I visit the cart page
Then I should not see the "Empty cart" button
# Many more Scenario's
Run Code Online (Sandbox Code Playgroud)
填写的细节越多,这个"空车"组就越长.我想知道,"清空车"应该被视为独立功能吗?这将导致许多功能,所有功能都包含一些场景.该方案的则变得更像"上下文".像这样:
Feature: Emptying Cart
So that I can reconsider my shopping-spree
As a user
I want to empty my cart
Scenario: with a filled cart
Given 2 products in my cart
When I visit the cart page
And I press "Empty cart"
Then I should see the text:
"""
Your cart is empty.
"""
Scenario: with an empty cart
Given 0 products in my cart
When I visit the cart page
Then I should not see the "Empty cart" button
Run Code Online (Sandbox Code Playgroud)
制作特色的好指南是什么?我什么时候应该将Scenario重组为他们自己的功能?一个功能通常有多少个场景?
你可以用陈述性语言而不是命令式语言来缩短你的场景 - 甚至可以摆脱一些场景.
例如:
Given the cart has two products in
When I empty the cart
Then I should see it has nothing in it.
Run Code Online (Sandbox Code Playgroud)
这可能与实际的购物车一样真实.它没有实现细节.你可以看到一些步骤对应于你的一个以上; 这是一件好事,因为它使代码中的复杂性更加可维护.
如果我们用这种方式表达你的其他场景,我们得到:
Given my cart is empty
Then I should not be able to empty it again.
Run Code Online (Sandbox Code Playgroud)
这里没有"何时",因为"那个"对于那个状态来说就是真的.
但是你真的需要这种情况吗?是否可以杀死你或你的公司以释放已经空车的能力?这基本上是美学的.它被添加以使UI更易于使用,并且判断UI是否可用的唯一方法是实际使用它.如果你发现这种情况,我建议把它们拿出来.您始终可以对导致启用或禁用按钮的逻辑进行单元测试.
如果您更改为这种语言风格,那么删除任何仅测试美学和可用性的场景,您会发现您的功能文件变得更小,更小.
我还建议将最有趣或最令人惊讶的场景放在顶部.例如,这将是一个更有趣的场景(是的,它有两个"whens",因为它描述了与两个不同用户之间的交互相关的行为):
Given a user put a copy of "Moby Dick" in his cart
When the last copy of "Moby Dick" is sold
And the user comes back to look at his cart
Then it should tell him, "Sorry, this item is currently out of stock."
Run Code Online (Sandbox Code Playgroud)
这会更有趣:
Given a user put a new copy of "Moby Dick" in his cart
And there are second-hand copies of "Moby Dick" available
When the last new copy of "Moby Dick" is sold
And the user comes back to look at his cart
Then it should tell him, "Sorry, this item is currently out of stock."
And it should also show the way to the second-hand copies with,
"2nd hand copies are available."
Run Code Online (Sandbox Code Playgroud)
这将是一个差异化的情景; 您的商店与其他商店的不同之处,因此对商家非常感兴趣.
通过将最有趣的那些放在顶部,功能文件很长并不重要.我们都知道购物车如何购买和销售商品,我们也不会在那个阶段读到底部的情景.
Andy Waite说他没有硬规则是正确的,所以要靠耳朵来玩; 如果它不起作用,做一些不同的事情.希望这些提示也会有所帮助.