我正在尝试测试使用某些 config()
值的中间件,但它不起作用。我得到了
Illuminate\Contracts\Container\BindingResolutionException: Target class [config] does not exist.
这不是框架中的新问题( https://github.com/laravel/framework/issues/9733 ),但我就是无法让它工作,并且它以一种令人不满意的方式关闭。
我已经尝试过:
- 不使用配置助手,而是使用 Facade,但测试环境中未设置 Facade 根。
- 使用配置存储库本身的新实例,但是……它是空的。
有什么想法吗?
我的中间件的句柄函数:
public function handle($request, Closure $next)
{
$result = $this->apiCall()
if($result->fails) { // Evaluates to true
throw new \Exception('message with config value'.config('option')); // throws error
}
return $next($request);
}
测试功能
public function testMiddleware()
{
$request = Request::create('/login', 'POST');
$middleware = new MyMiddleware;
$this->expectException(\Exception::class);
$response = $middleware->handle($request, function() {});
}
最佳答案
这个测试不起作用的原因非常愚蠢。我使用命令创建了测试
php artisan make:test MyTestCase --unit
由于我传递了 --unit
标志,它扩展了 PHPUnit 的 TestCase 类,而不是 Laravel 自己的。
关于php - 尝试在测试函数 Laravel 6 中使用配置值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59359765/