Behat:如何清理场景中创建的数据库记录

Behat: How to clean up database records created in the scenario

我有以下场景:

    @mink:selenium2
  Scenario: Login
    Given there are the following users:
      | username | password | email               |
      | admin    | 1234     | admin@socialcar.com |
    When I am on "/login"
    And I fill in "username" with "admin"
    And I fill in "password" with "1234"
    And I press "Login"
    Then I should be on "/admin"

所以我想要一个 cleanupUsers 作为 @AfterScenario,我可以在其中清除场景中插入的任何内容。那么如何访问用户的TableNode呢?

根据the doc:挂钩"will also have access to any object properties you set during your scenario"。

所以我认为您只需要在 class 的代码中将 TableNode(或更好,只是创建的用户 ID)保存在上下文 class 的 属性 中=11=]步骤,然后在cleanupUsers.

中使用

您可以将您的用户保存在私有 属性 中,以便稍后在 a hook 中访问它们:

private $users;

/**
 * @Given there are the following users:
 */
public function thereAreFollowingUsers(TableNode $table)
{
    $this->users = $table;

    // ...
}

/**
 * @AfterScenario
 */
public function cleanupUsers(AfterScenarioScope $scope)
{
    if (null !== $this->users) {
        // do the cleanups
        // ...

        // reset the property
        $this->users = null;
    }
}