代码之家  ›  专栏  ›  技术社区  ›  abbood

如何处理在“then”阶段有许多步骤的小黄瓜场景

  •  0
  • abbood  · 技术社区  · 6 年前

    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
    

    这看起来真难看。但现在我不知道怎么把它分开。做一个 什么时候 然后 配对(我用的是Behat, which has 鉴于 , 在每个场景中(在背景之后)。

    建议?

    2 回复  |  直到 6 年前
        1
  •  1
  •   Jon Acker    6 年前

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

    鉴于 步骤(如 Arrange 步进单元测试)用于在给定状态下设置系统。这个州建立在 鉴于 Then 步进)

    例如:

    那么 步骤)。

    鉴于 与此场景的结果直接相关的步骤

    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
  •  0
  •   Stratadox    6 年前

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

    一个场景,一个行为!

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

    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

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

    毕竟,通知ops是另一回事 特征