7

For laravel API I have write the test cases. 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(); 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.

Narayan
  • 1,670
  • 1
  • 19
  • 37

4 Answers4

2

It is a 404 error, so your webpage is not found :

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" !

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

BendaThierry.com
  • 2,080
  • 1
  • 15
  • 17
  • I have tried with `/index.php/api/company` only. Also, I am able to access this API from the postman – Narayan Aug 10 '20 at 13:40
  • Which url are you putting inside postman ? Do you make some checks inside your action code and allow only some HTTP methods ie GET rejected, POST ok ? – BendaThierry.com Aug 10 '20 at 13:43
  • I am putting the same URL with domain name in postman and its working correctly. Also, I have tried to get, and post issue is the same. – Narayan Aug 10 '20 at 13:49
  • hmm strange... if I were you I will check the action code and the route config very closely to check twice everything is well formatted. and I will do some tests with `/api/company`. – BendaThierry.com Aug 10 '20 at 13:54
  • Did you follow some tutorial like these one : https://medium.com/@mscherrenberg/unit-testing-your-api-in-laravel-5-6-7172bcdc593d – BendaThierry.com Aug 10 '20 at 13:55
  • Maybe these one looks better : https://www.twilio.com/blog/unit-testing-laravel-api-phpunit – BendaThierry.com Aug 10 '20 at 13:56
  • I am tried the same but no success. The only difference is I am not using migration. – Narayan Aug 10 '20 at 14:12
1

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. So index.php shouldn't be a valid route for laravel.

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

1

It seems to me that PHPUnit can't find not only this URL but the whole website. 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. Usually, I set it as APP_URL=http://localhost, and it works for me.

SeemsLegit
  • 97
  • 3
0

A 404 code means there was nothing found at the specified route. It could be the route you specified index.php/api/company. Try routing to [host:port]/api/company

Igiri David
  • 131
  • 4