我刚刚开始在我的 laravel 应用程序上编写 PHPUnit 路由测试,它通过浏览器和 Postman 运行良好,但不是通过 PHPunit。
示例:在本次测试中
public function test_getAll(){
$this->withoutExceptionHandling(); // If i comment this line I get the 404 and not the error shown below
$response = $this->get('/api/users');
$response->assertStatus(401);
}
我得到:
PHPUnit 8.5.3 by Sebastian Bergmann and contributors.
.E 2 / 2 (100%)
Time: 1.89 seconds, Memory: 8.00 MB
There was 1 error:
1) Tests\Feature\Users\UserRoute_SuperAdminTest::test_getAll
Symfony\Component\HttpKernel\Exception\NotFoundHttpException: GET http://localhost/cms/api/users
E:\www\projects\cms-php\vendor\laravel\framework\src\Illuminate\Foundation\Testing\Concerns\InteractsWithExceptionHandling.php:126
E:\www\projects\cms-php\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php:415
E:\www\projects\cms-php\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php:113
E:\www\projects\cms-php\vendor\laravel\framework\src\Illuminate\Foundation\Testing\Concerns\MakesHttpRequests.php:468
E:\www\projects\cms-php\vendor\laravel\framework\src\Illuminate\Foundation\Testing\Concerns\MakesHttpRequests.php:258
E:\www\projects\cms-php\tests\Feature\Users\UsersRoute-SuperAdmin_Test.php:45
奇怪的是:如果我将 URL 更改为:
$response = $this->get('http://anythingatallinhere/api/users');
我得到了我应该得到的 401 响应。
更多上下文信息来解决问题。
我的 env APP_URL 是APP_URL=http://localhost/cms,我正在以这种方式动态注册路由:我有一个 CoreServiceProvider ,其启动过程如下:
public function boot()
{
[...]
$moduleController = app()->make(ModuleController::class);
$moduleController->registerCoreRoutes();
[...]
如果我使用php artisan route:list
它们,它们也都已正确注册。
翻过高山走不出你
芜湖不芜