1

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?

2
  • 1
    That's a stack trace but I don't actually see the error. Commented Oct 20, 2015 at 16:19
  • Error is NotFoundHttpException. I'll ammend snippet when I get to desktop computer Commented Oct 20, 2015 at 16:30

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.