【问题标题】:How to us actingAs() with Sentinel我们如何使用 Sentinel 来代理()
【发布时间】:2019-01-04 18:52:52
【问题描述】:

我正在测试一个要求用户登录的 Laravel 5.1 页面。我的项目使用 Cartalyst/Sentinel 打包进行身份验证。

我试过了,但我不知道用户已登录。

public function testPageWithLogin()
{
    $user = Sentinel::findById(2);

    $this->actingAs($user)
         ->withSession([])
         ->visit('/page')
         ->dontSee('Whoops')
         ->dontSee('login');
}

我该怎么做才能让用户被视为已登录?

【问题讨论】:

    标签: laravel authentication testing laravel-5.1 cartalyst-sentinel


    【解决方案1】:

    我忘记使用 Sentinel::login 方法登录用户。该用户是合法的,只是没有被视为已登录。

    这是应该做的。

    public function testPageWithLogin()
    {
        $user = Sentinel::findById(2);
        Sentinel::login($user);
    
        $this->actingAs($user)
             ->withSession([])
             ->visit('/page')
             ->dontSee('Whoops')
             ->dontSee('login');
    }
    

    【讨论】:

    • Sentinel 的用户模型必须实现 Authenticatable Contract:..\TestCase::actingAs() must be an instance of Illuminate\Contracts\Auth\Authenticatable
    猜你喜欢
    • 2020-01-15
    • 1970-01-01
    • 2020-01-04
    • 2017-02-13
    • 2011-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多