【发布时间】:2020-11-30 02:20:50
【问题描述】:
对于 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);
}
我搜索了错误并尝试了许多解决方案,但都无法成功。任何人都请告诉我这是什么问题。
【问题讨论】:
-
发布到
/api/company,HTTP 测试将查找路由并直接调用它。 -
@apokryfos 尝试但没有成功
-
你能在
routes/api.php中显示使用你的路由声明吗? -
@gbalduzzi 我在 routes/api.php 中的路线是
Route::any('company','CompanyController@companyindex');
标签: php laravel phpunit testcase laravel-7.x