【问题标题】:How to handle gherkin scenarios that have many steps in the 'then' stage如何处理在“then”阶段有很多步骤的小黄瓜场景
【发布时间】:2018-10-14 19:37:02
【问题描述】:

他们say认为BDD的基本规则是:

一种情景,一种行为!

这很好,但事实是我有一个动作会触发系统中的许多不同的事情。我必须验证所有这些事情都会发生。这是我的小黄瓜:

Scenario: Buying a single product to be delivered now
    Given I am in the checkout page 
    When I checkout the order
    Then the order should be created to the specified address 
    And the order should be set in pending state
    And ops must be notified via a slack notification
    And A shopper must be auto assigned the order via a push notification
    And A retailer must be notified about the order via a push notification 
    And A transaction must be recorded in the payment gateway
    And My wallet should be deducted by the payment amount

这看起来真的很难看。但现在我不知道如何分开它。做一个 Background 似乎并没有削减它,因为在背景中,您只是为多个场景设置了基础,每个场景都有它的 when然后 对(在我的情况下,我使用 Behat,which has 一个 givenwhenthen 在每个场景之后背景)。

建议?

【问题讨论】:

    标签: php bdd behat


    【解决方案1】:

    除了@Stratadox 的答案(按行为划分场景)-Given 步骤绝不应提及用户界面或用户旅程中的阶段。用户所在的页面与业务逻辑无关。

    Given 步骤(如单元测试中的Arrange 步骤)用于将系统设置为给定状态。在Given 步骤中设置的这个状态决定了结果(Then 步骤)

    例如:

    “订单应该创建到指定的地址”——这个结果可能是由于客户正确输入了他们的地址(在这种情况下,可能需要指定实际地址以便可以断言)在Then 步骤中)。

    “我的钱包应该从支付金额中扣除”——为了在你的钱包充值后断言系统处于正确状态,我们可能会检查某种数据持久性,我们还会检查具体金额。多少钱?它是您在Given 步骤中指定的数量,与此方案的结果直接相关

    Scenario: Being charged the correct amount on successful purchase of product
       Given I have £10 in my wallet
       When I purchase a Toaster Oven for £4
       Then I should have £6 left in my wallet
    

    “必须通过推送通知将订单自动分配给购物者”——这是可以断言为“成功购买”检查的一部分的结果。唯一取决于的是购买是否成功。您可以将诸如“购物者通知,零售商通知交易记录”之类的断言捆绑在一起 - 并在您的场景中给它们一个名称,一行例如:

    Given Jon has the following items in his basket:
      | Macbook Pro | £3000 |
    When Jon checks out his basket
    Then a purchase of "xyz" for £3000 by Jon should have gone through
    

    这里的最后一步意味着在后台执行三个断言。这很好,因为每个人都知道“购买通过”意味着什么

    【讨论】:

      【解决方案2】:

      解决此问题的最直接方法是遵循您在问题中引用的建议:

      一种情景,一种行为!

      要实施此建议,只需将臃肿的场景拆分为多个较小的场景。

      例如:

      Scenario: Buying a single product to be delivered now
          Given I am in the checkout page 
          When I checkout the order
          Then the order should be created to the specified address 
          And the order should be set in pending state
      
      Scenario: Notifying ops of the purchase
          Given I am in the checkout page 
          When I checkout the order
          Then ops must be notified via a slack notification
      

      ...等等。

      您的场景中的许多given 彼此之间并没有太大关系。它们有一些时间关系,因为它们都恰好发生在下订单之后,但仅此而已。

      您甚至可以更进一步,考虑将它们分组为不同的功能。

      毕竟,通知运营商是与处理付款不同的功能,而后者又是不同的库存计算方式。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-03-13
        相关资源
        最近更新 更多