讨论数量:
解决方案(一):
在需要跳过的中间件handle方法中加上一个判断:
if (env('APP_ENV') === 'testing') {
return $next($request);
}
解决方案(二):
a). Laravel 版本从 5.5 开始支持withoutMiddleware
方法;可以指定要禁用的中间件,而不是全部禁用它们。
$this->withoutMiddleware(\App\Http\Middleware\VerifyCsrfToken::class);
b). 如果Laravel版本小于5.5,则可以通过将更新的方法添加到基本TestCase类中以覆盖框架TestCase的功能来实现相同的功能。
c). 如果php版本大于7,可以将以下内容添加到TestCase类中,您将能够使用上述相同的方法调用。此功能使用PHP7中引入的匿名类。
/**
* Disable middleware for the test.
*
* @param string|array|null $middleware
* @return $this
*/
public function withoutMiddleware($middleware = null)
{
if (is_null($middleware)) {
$this->app->instance('middleware.disable', true);
return $this;
}
foreach ((array) $middleware as $abstract) {
$this->app->instance($abstract, new class {
public function handle($request, $next)
{
return $next($request);
}
});
}
return $this;
}
转自 @tsin 提供的:https://stackoverflow.com/questions/343573... 解决方法
或许这个可以帮到你:https://stackoverflow.com/questions/343573...