因此,在运行时会经常出现来自Authentication和CSRF的错误phpunit
。
因此,在TestCase中,我们使用:
use WithoutMiddleware;
问题在于,当表单失败时,它通常会返回一条Flash消息和一个旧输入。我们已禁用了所有中间件,因此我们无权访问Input::old('username');
或收到Flash消息。
此外,我们对此失败的表单发布的测试返回:
Caused by
exception 'RuntimeException' with message 'Session store not set on request.
有没有一种方法可以启用会话中间件并禁用其他所有功能。
我发现做到这一点的最佳方法不是通过使用WithoutMiddleware
trait,而是通过修改要禁用的中间件。例如,如果要VerifyCsrfToken
在测试中禁用中间件功能,则可以执行以下操作。
在内app/Http/Middleware/VerifyCsrfToken.php
,添加一种handle
检查APP_ENV
测试的方法。
public function handle($request, Closure $next)
{
if (env('APP_ENV') === 'testing') {
return $next($request);
}
return parent::handle($request, $next);
}
这将覆盖内的handle
方法Illuminate\Foundation\Http\Middleware\VerifyCsrfToken
,从而完全禁用该功能。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句