简体   繁体   English

Laravel PHPUnit 返回 404

[英]Laravel PHPUnit returning 404

For laravel API I have write the test cases.对于 laravel API 我已经编写了测试用例。 But whenever I am running the test cases it always failed with below error,但是每当我运行测试用例时,它总是失败并出现以下错误,

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

After adding the $this->withoutExceptionHandling();添加$this->withoutExceptionHandling();之后to testcase code it return the below error,测试用例代码返回以下错误,

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

My Code in Test File is,我在测试文件中的代码是,

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

I have googled the error and tried many solutions but unable to succeed.我已经搜索了错误并尝试了许多解决方案但无法成功。 Anyone please let me know whats the issue.任何人请让我知道是什么问题。

It is a 404 error, so your webpage is not found:这是一个 404 错误,所以没有找到您的网页:

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

Bad way to go, post to /api/company or with /index.php/api/company but not with "domainname" ! go 的错误方式,发布到/api/company或使用/index.php/api/company但不使用“域名”!

If it is not working, check your route config for that api route.如果它不起作用,请检查您的路线配置中的 api 路线。 Are your controller and action declared well?您的 controller 和行动声明是否正确?

A 404 code means there was nothing found at the specified route. 404 代码表示在指定路线上找不到任何东西。 It could be the route you specified index.php/api/company .它可能是您指定的路线index.php/api/company Try routing to [host:port]/api/company尝试路由到[host:port]/api/company

By default, laravel's routing does not explicitly show that it is a php file rather than it has dedicated endpoints which you decide on the routing file.默认情况下,laravel 的路由没有明确显示它是一个 php 文件,而不是它具有您在路由文件中决定的专用端点。 So index.php shouldn't be a valid route for laravel.所以 index.php 不应该是 laravel 的有效路由。

A 404 error status code refers to a Not Found Error. 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. 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.

I suggest using php artisan route:list on the base path of the project to properly see that the route you wanted is registered and match it from there.我建议在项目的基本路径上使用php artisan route:list以正确查看您想要的路由已注册并从那里匹配。

It seems to me that PHPUnit can't find not only this URL but the whole website.在我看来,PHPUnit 不仅找不到这个 URL,而且找不到整个网站。 You could check it by request to "/" in your test.您可以在测试中通过请求“/”来检查它。

If I'm right, first of all, I'll suggest checking the param APP_URL in your .env.testing .如果我是对的,首先,我建议您检查APP_URL中的参数.env.testing Usually, I set it as APP_URL=http://localhost , and it works for me.通常,我将其设置为APP_URL=http://localhost ,它适用于我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM