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

时间:2015-09-08 11:26:41

标签: php symfony behat

我有以下情况:

    @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?

2 个答案:

答案 0 :(得分:2)

您可以将用户保存在私有媒体资源中,以便稍后在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;
    }
}

答案 1 :(得分:0)

根据the doc:hooks"还可以访问您在场景中设置的任何对象属性"。

所以我认为您只需要在TableNode步骤的代码中将Given there are the following users(或者更好,只是创建的用户ID)保存在上下文类的属性中,然后使用它在cleanupUsers