3

如何手动确保用户未在 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

4

2 回答 2

4

Here's the custom step you can use:

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

But you probably need to update your steps order:

Given I am not logged in
  And I am on "/login"
于 2012-08-22T17:37:03.237 回答
2

只需访问“/logout”,您将被注销。在转到“/login”之前执行此操作

于 2012-08-22T17:00:08.140 回答