Reputation: 543
I am trying to run a sequence of API calls in Laravel test but for some reason calling POST method breaks the state of test object when called with default values according to documentation of the test class
$userData = json_encode([
'username' => 'Bender',
'password' => 'CorrectHorseBatteryStaple123#',
'questions' => ['What is the airspeed of unladen swallow?'],
'answers' => ['African or european?'],
]);
$response1 = $this->call('GET', 'languages');
echo $response1;
$response = $this->call('POST', 'accounts/create',array(),array(),array(),$userData);
echo $response;
$this->assertResponseOk();
$response1 = $this->call('GET', 'languages'); //this is line 30
echo $response1;
$this->assertResponseOk();
First GET call, and POST call finish normally and return expected results. But second GET call fails. (GET and POST calls touch completely different parts of code and DB and should not affect each other.)
Error I get is
1) Project\Test\AccountsTest::testSample
Symfony\Component\HttpKernel\Exception\NotFoundHttpException:
/var/www/project/project-backend/vendor/laravel/framework/src/Illuminate/Routing/RouteCollection.php:148
/var/www/project/project-backend/vendor/laravel/framework/src/Illuminate/Routing/Router.php:1054
/var/www/project/project-backend/vendor/laravel/framework/src/Illuminate/Routing/Router.php:1022
/var/www/project/project-backend/vendor/laravel/framework/src/Illuminate/Routing/Router.php:1001
/var/www/project/project-backend/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:775
/var/www/project/project-backend/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:745
/var/www/project/project-backend/vendor/symfony/http-kernel/Symfony/Component/HttpKernel/Client.php:81
/var/www/project/project-backend/vendor/symfony/browser-kit/Symfony/Component/BrowserKit/Client.php:327
/var/www/project/project-backend/vendor/laravel/framework/src/Illuminate/Foundation/Testing/ApplicationTrait.php:51
/var/www/project/project-backend/test/Routes/AccountsTest.php:30
/var/www/project/project-backend/test/Routes/AccountsTest.php:49
Is there any special value I should pass as params 3,4,5 in POST call to make subsequent calls work?
Upvotes: 1
Views: 561
Reputation: 543
Solved it.
The problem was not in POST or JSON but in fact that code in
symfony/browser-kit/Symfony/Component/BrowserKit/Client.php
tried to be smart and kept history of previous requests.
After calling accounts/create
, and then languages
it actually went to accounts/languages
Adding / at the beginning of $uri
parameter did the trick.
$response1 = $this->call('GET', '/languages');
$response = $this->call('POST', '/accounts/create',array(),array(),array(),$userData);
$response1 = $this->call('GET', '/languages'); //this now works
Upvotes: 2