我剛剛開始在我的 laravel 應(yīng)用程序上編寫 PHPUnit 路由測試,它通過瀏覽器和 Postman 運(yùn)行良好,但不是通過 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 MBThere was 1 error:1) Tests\Feature\Users\UserRoute_SuperAdminTest::test_getAllSymfony\Component\HttpKernel\Exception\NotFoundHttpException: GET http://localhost/cms/api/usersE:\www\projects\cms-php\vendor\laravel\framework\src\Illuminate\Foundation\Testing\Concerns\InteractsWithExceptionHandling.php:126E:\www\projects\cms-php\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php:415E:\www\projects\cms-php\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php:113E:\www\projects\cms-php\vendor\laravel\framework\src\Illuminate\Foundation\Testing\Concerns\MakesHttpRequests.php:468E:\www\projects\cms-php\vendor\laravel\framework\src\Illuminate\Foundation\Testing\Concerns\MakesHttpRequests.php:258E:\www\projects\cms-php\tests\Feature\Users\UsersRoute-SuperAdmin_Test.php:45奇怪的是:如果我將 URL 更改為:$response = $this->get('http://anythingatallinhere/api/users');我得到了我應(yīng)該得到的 401 響應(yīng)。更多上下文信息來解決問題。我的 env APP_URL 是APP_URL=http://localhost/cms,我正在以這種方式動態(tài)注冊路由:我有一個(gè) CoreServiceProvider ,其啟動過程如下:public function boot() { [...] $moduleController = app()->make(ModuleController::class); $moduleController->registerCoreRoutes(); [...]如果我使用php artisan route:list它們,它們也都已正確注冊。
2 回答

翻過高山走不出你
TA貢獻(xiàn)1875條經(jīng)驗(yàn) 獲得超3個(gè)贊
我最近遇到了類似的問題,但運(yùn)行這些后它起作用了:
php artisan config:clear php artisan config:cache
特別是如果您更改了 .env 文件中剛剛更改的 APP_URL。
您還可以嘗試在 phpunit.xml 文件中processIsolation
設(shè)置:true
<phpunit backupGlobals="false" backupStaticAttributes="false" bootstrap="bootstrap/autoload.php" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="true" stopOnFailure="false">
編輯: 如果上述方法都不起作用,您還可以創(chuàng)建另一個(gè).env.testing
并將您的設(shè)置APP_URL
為http://localhost
. PHPUnit 將使用該文件中的變量。無論您的應(yīng)用程序的實(shí)際 URL 是什么,這都有效

蕪湖不蕪
TA貢獻(xiàn)1796條經(jīng)驗(yàn) 獲得超7個(gè)贊
最簡單的方法是通過添加以下行來http://localhost
在phpunit.xml
文件中設(shè)置 APP_URL:
<server name="APP_URL" value="http://localhost"/>
如果您希望變量在.env
測試期間仍然可用,則此方法是最好的。創(chuàng)建您自己的.env.testing
文件將覆蓋所有.env
變量。
- 2 回答
- 0 關(guān)注
- 153 瀏覽
添加回答
舉報(bào)
0/150
提交
取消