Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
测试检查登录到laravel的用户_Laravel_Laravel 5_Phpunit - Fatal编程技术网

测试检查登录到laravel的用户

测试检查登录到laravel的用户,laravel,laravel-5,phpunit,Laravel,Laravel 5,Phpunit,我有一个简单的工作路线: if(Auth::check()){ 返回响应()->json('true'); }否则{ 返回响应()->json('false'); } 我需要使用PHPUnit中的类似内容测试此函数: $this->get('auth/checkLoggedIn') ->参见JSON([true]); 如何模拟测试用户已登录?使用actngAs($user\u id)模拟已登录用户 一个更完整的例子: // simulate ajax request $t

我有一个简单的工作路线:

if(Auth::check()){
返回响应()->json('true');
}否则{
返回响应()->json('false');
}
我需要使用PHPUnit中的类似内容测试此函数:

$this->get('auth/checkLoggedIn')
->参见JSON([true]);
如何模拟测试用户已登录?

使用
actngAs($user\u id)
模拟已登录用户

一个更完整的例子:

  // simulate ajax request
        $this->actingAs($user_id)               
            ->post(route('api.bookmark.store'), [
                'event' => $this->event->id,
                '_token' => csrf_token()
            ]);

返回类似以下内容的响应:

if (Auth::check()) {
    return response()->json(["logged"=>true]);
} else {
    return response()->json(["logged"=>false]);
}
使用laravel的工厂模型,创建一个示例用户。为了避免令牌不匹配错误,可以使用WithoutMiddleware特性。但是如果它是一个
GET
请求,您就不需要它了。但是如果是
帖子
,那么您可能需要它

所以你可以用任何一个

use Illuminate\Foundation\Testing\WithoutMiddleware;

UserTest extends TestCase {

use WithoutMiddleware;

/**
*@test
*/
public function it_tests_authentication()
{
    $user = factory(User::class)->create();

    $this->actingAs($user);

    $this->post('auth/checkLoggedIn')
             ->seeJson(["logged"=>true]);

//or GET depending on your route

        $this->get('auth/checkLoggedIn')
         ->seeJson(["logged"=>true]);

}