【问题标题】:How I can authenticate in codeception functional tests using Sentinel?如何使用 Sentinel 在代码接收功能测试中进行身份验证?
【发布时间】:2016-03-08 02:01:54
【问题描述】:

我正在尝试使用 codeception 测试 laravel 应用程序。 在 Laravel 内部,我使用 Sentinel 进行身份验证。 我在测试中对用户进行身份验证时遇到问题。
以下示例均无效:

$I->authenticateTestUser($I);
$user = \App\User::where('email','=','test@test.com')->first();
$I->amLoggedAs($user);
$I->canSeeAuthentication();


$credentials = [
    'email'    => 'test@test.com',
    'password' => '****',
];
Sentinel::authenticate($credentials, true);
$I->canSeeAuthentication();


$I->amOnPage('/login');
$I->fillField('email', $email);
$I->fillField('password', $password);
$I->click('Login');
$I->canSeeAuthentication();

如何在代码接收功能测试中进行身份验证?

【问题讨论】:

  • 不能使用Sentinel::authenticatecodeception实例与application不一样。

标签: laravel codeception


【解决方案1】:

你只需要在 _before 方法中使用 sentinel :

private $_user;
private $_credentials;

public function _before(FunctionalTester $I)
{
    // we create a user
    $this->_credentials = [
        'last_name'  => 'Test',
        'first_name' => 'test',
        'email'      => 'test@test.fr',
        'password'   => 'password',
    ];
    $this->_user = \Sentinel::register($this->_credentials, true);

    // we log this user
    \Sentinel::authenticate($this->_credentials);
}

您将能够在测试中重复使用您的用户或凭据。

【讨论】:

  • 我试过了。不工作:'$I->canSeeAuthentication();'失败并显示消息“没有经过身份验证的用户”
  • _before方法的最后,测试添加dd(\Sentinel::getUser())。如果你没有得到false 作为结果,你的用户就会被登录。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-10-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-15
  • 2023-03-31
相关资源
最近更新 更多