Laravel PHPUnit 返回 404

本教程将介绍Laravel PHPUnit 返回 404的处理方法,这篇教程是从别的地方看到的,然后加了一些国外程序员的疑问与解答,希望能对你有所帮助,好了,下面开始学习吧。

Laravel PHPUnit 返回 404 教程 第1张

问题描述

对于 laravel API,我已经编写了测试用例.但是每当我运行测试用例时,它总是失败并出现以下错误,

1) 测试功能CompanyTest::orgTest预期状态码 200 但收到 404.无法断言 200 与 404 相同.

$this->withoutExceptionHandling(); 添加到测试用例代码后,它会返回以下错误,

1) 测试功能CompanyTest::orgTestSymfonyComponentHttpKernelExceptionNotFoundHttpException: POST 域名/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

我在测试文件中的代码是,

公共函数 orgTest(){$requestData = [组织 ID"=>10,offset"=>1,limit"=>10,notificationId"=>""];$response = $this->withoutExceptionHandling();$response->postJson('/index.php/api/company',$requestData);$response->assertStatus(200);}

我已经搜索了错误并尝试了许多解决方案但无法成功.任何人请告诉我是什么问题.

解决方案

是404错误,所以没有找到你的网页:

SymfonyComponentHttpKernelExceptionNotFoundHttpException: POST 域名/index.php/api/company

不好的方式,发布到 /api/company 或使用 /index.php/api/company 但不是使用域名"!

如果它不起作用,请检查该 api 路由的路由配置.你的控制器和动作声明得好吗?

For laravel API I have write the test cases. But whenever I am running the test cases it always failed with below error,

1) TestsFeatureCompanyTest::orgTest
Expected status code 200 but received 404.
Failed asserting that 200 is identical to 404.

After adding the $this->withoutExceptionHandling(); to testcase code it return the below error,

1) TestsFeatureCompanyTest::orgTest
SymfonyComponentHttpKernelExceptionNotFoundHttpException: 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 :

SymfonyComponentHttpKernelExceptionNotFoundHttpException: POST domainname/index.php/api/company

Bad way to go, post to /api/company or with /index.php/api/company but not with "domainname" !

If it is not working, check your route config for that api route. Are your controller and action declared well ?

好了关于Laravel PHPUnit 返回 404的教程就到这里就结束了,希望趣模板源码网找到的这篇技术文章能帮助到大家,更多技术教程可以在站内搜索。