繁体   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