Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.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

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
Unit testing 如何在Laravel 5中模拟tymondesigns/jwt auth?_Unit Testing_Laravel_Laravel 5 - Fatal编程技术网

Unit testing 如何在Laravel 5中模拟tymondesigns/jwt auth?

Unit testing 如何在Laravel 5中模拟tymondesigns/jwt auth?,unit-testing,laravel,laravel-5,Unit Testing,Laravel,Laravel 5,我刚刚添加了tokenauth来支持,因此我的测试用例失败了,因为headers参数上没有token。如何模拟(使用mockry)组件以绕过此问题 注意:$this->be()不起作用另一种方法是在通过特定用户的测试执行过程中验证请求。这是怎么做到的: #tests/TestCase.php /** *与API交互所需的返回请求头。 * *@return数组的头。 */ 受保护的函数头($user=null) { $headers=['Accept'=>'application/json'];

我刚刚添加了tokenauth来支持,因此我的测试用例失败了,因为headers参数上没有token。如何模拟(使用
mockry
)组件以绕过此问题


注意:
$this->be()
不起作用

另一种方法是在通过特定用户的测试执行过程中验证请求。这是怎么做到的:

#tests/TestCase.php
/**
*与API交互所需的返回请求头。
*
*@return数组的头。
*/
受保护的函数头($user=null)
{
$headers=['Accept'=>'application/json'];
如果(!为null($user)){
$token=JWTAuth::fromUser($user);
JWTAuth::setToken($token);
$headers['Authorization']='Bearer'.$token;
}
返回$headers;
}
然后在我的测试中,我使用它如下:

#tests/StuffTest.php
/**
*测试:GET/api/stuff。
*/
公共函数testIndex()
{
$url='/api/stuff';
//测试未经验证的访问。
$this->get($url,$this->headers())
->资产负债表(400);
//测试经过身份验证的访问。
$this->get($url,$this->headers(User::first()))
->seeJson()
->assertResponseOk();
}

希望这对你们大家都有帮助。快乐编码

你找到解决这个问题的方法了吗?我通读了整个源代码,并模仿了jwtauth的每个方法调用。但是看起来Laravel5.1有一个特点就是绕过了中间件,你可能想看看,我也对解决这个问题很感兴趣。已尝试禁用中间件,但无法使其工作。你解决了这个问题吗@克里斯托弗兰基斯科