我如何使用Sentinel在代码功能测试中进行身份验证?

时间:2016-02-02 21:33:02

标签: laravel 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();

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

1 个答案:

答案 0 :(得分:0)

你只需要在_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);
}

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