Symfony 2 BDD Behat context "I am not logged in"



在BDD Behat场景中,我如何手动确保用户没有登录到我的FeatureContext类中?例如,我有这样的场景:

Scenario: passing wrong values will not get me in
Given I am on "/login"
And I am not logged in
When I fill in "login" with "KtokolwiekWidzialKtokolwiekWie"
When I fill in "password" with "root_as_allways"
When I press "Login"
Then I should be on "/"
And I should see "Wrong login or password"

我定义了这个上下文:

/**
 * @Given /^I am not logged in$/
 */
public function iAmNotLoggedIn()
{
    # logout currently login user, if any
    $request = $this->kernel->getContainer()->get('request');
    $request->getSession()->invalidate();
}

但是在运行此功能测试时,这给了我一个错误。

您不能创建非活动范围("请求")的服务("请求")。

注意:我使用的是Symfony 2.1 RC-1

以下是您可以使用的自定义步骤:

/**
 * @Given /^I am not logged in$/
 */
public function iAmNotLoggedIn()
{
    $this->getMink()
        ->getSession()
        ->visit($this->locatePath('/logout'))
    ;
}

但你可能需要更新你的步骤顺序:

Given I am not logged in
  And I am on "/login"

只需访问"/logoout",您就会被注销。在进入"/login"之前执行此操作

相关内容

最新更新