簡體   English   中英

Laravel PHPUnit 返回 404

[英]Laravel PHPUnit returning 404

對於 laravel API 我已經編寫了測試用例。 但是每當我運行測試用例時,它總是失敗並出現以下錯誤,

1) Tests\Feature\CompanyTest::orgTest
Expected status code 200 but received 404.
Failed asserting that 200 is identical to 404.

添加$this->withoutExceptionHandling();之后測試用例代碼返回以下錯誤,

1) Tests\Feature\CompanyTest::orgTest
Symfony\Component\HttpKernel\Exception\NotFoundHttpException: POST domainname/index.php/api/company

/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithExceptionHandling.php:126
/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:415
/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:113
/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php:507
/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php:473
/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php:332
/tests/Feature/CompanyTest.php:21

我在測試文件中的代碼是,

public function orgTest()
    {
        $requestData = ["organizationId" => 10,"offset"=>1,"limit"=>10,"notificationId"=>""];
        $response = $this->withoutExceptionHandling();
        $response->postJson('/index.php/api/company',$requestData);
        $response->assertStatus(200);
    }

我已經搜索了錯誤並嘗試了許多解決方案但無法成功。 任何人請讓我知道是什么問題。

這是一個 404 錯誤,所以沒有找到您的網頁:

Symfony\Component\HttpKernel\Exception\NotFoundHttpException: POST domainname/index.php/api/company

go 的錯誤方式,發布到/api/company或使用/index.php/api/company但不使用“域名”!

如果它不起作用,請檢查您的路線配置中的 api 路線。 您的 controller 和行動聲明是否正確?

404 代碼表示在指定路線上找不到任何東西。 它可能是您指定的路線index.php/api/company 嘗試路由到[host:port]/api/company

默認情況下,laravel 的路由沒有明確顯示它是一個 php 文件,而不是它具有您在路由文件中決定的專用端點。 所以 index.php 不應該是 laravel 的有效路由。

404 錯誤狀態代碼是指未找到錯誤。 Maybe the api url that you are trying to access does not match the one inside the routes file whether you are using web.php or api.php.

我建議在項目的基本路徑上使用php artisan route:list以正確查看您想要的路由已注冊並從那里匹配。

在我看來,PHPUnit 不僅找不到這個 URL,而且找不到整個網站。 您可以在測試中通過請求“/”來檢查它。

如果我是對的,首先,我建議您檢查APP_URL中的參數.env.testing 通常,我將其設置為APP_URL=http://localhost ,它適用於我。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM