【发布时间】:2021-11-04 20:55:58
【问题描述】:
我需要根据配置使我的路线有条件:
//routes/auth.php
if (config('auth.allow_registration')) {....
以上配置参数是在配置文件中设置的:
//config/auth.php
'allow_registration' => false,
一切正常,直到我尝试对其进行单元测试
public function test_registration_screen_can_be_rendered()
{
config()->set('auth.allow_registration', true);
$response = $this->get('/register');
$response->assertStatus(200);
}
测试用例失败了。
我了解更改配置后,我需要重新读取路由。但是怎么做呢?
我发现只有这个$this->refreshApplication(); 它应该重新读取路由,但它也会重新读取配置。
我怎样才能只重读路由,但保持修改后的配置不变?
【问题讨论】:
-
发生这种情况是因为当测试开始时,
application已经加载,因此路由已经整理好,您现在无法更改它。你在哪里使用if?而且您应该使用env而不是文字false,因此您可以尝试在setUp方法上更改它(更改env值)。但请分享更多信息。 -
感谢您的介入。
if位于routes/auth.php文件中 -
查看
refreshApplication()的来源。它在做什么?
标签: laravel phpunit laravel-8 laravel-routing laravel-config