samedi 27 février 2016

Check if the API returns a token in Laravel (Unit testing)

I try to write a test in Laravel. I submit 4 fields and I want to check if I receive a token as a response. I know I do but I don't know how to check for it in a test.

public function testSignup()
{

    $this->json('POST', '/api/auth/signup',[
        'prenom' => 'TestPrenom',
        'nom' => 'TestNom',
        'email' => 'courriel@courriel.com',
        'password' => 'motdepasse'
        ])->seeJson(['token']);

}

For reference, here is the error I get running the test:

There was 1 failure:

1) SignupTest::testSignup
Unable to find JSON fragment [["token"]] within [{"token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjEsImlzcyI6Imh0dHA6XC9cL2xvY2FsaG9zdFwvYXBpXC9hdXRoXC9zaWdudXAiLCJpYXQiOjE0NTY1Nzk2MTAsImV4cCI6MTQ1NjU4MzIxMCwibmJmIjoxNDU2NTc5NjEwLCJqdGkiOiI3YTlmODFmZDA3ZDc2NDdlZTA4NTE5NTgzYWFhZTRlYSJ9.P3SUqxpf1vTfWmSFH4aTC9GESloYTutFNOBpw0xgrjA"}].
Failed asserting that false is true.

/home/ubuntu/workspace/zoneapi/vendor/laravel/framework/src/Illuminate/Foundation/Testing/CrawlerTrait.php:262
/home/ubuntu/workspace/zoneapi/vendor/laravel/framework/src/Illuminate/Foundation/Testing/CrawlerTrait.php:221
/home/ubuntu/workspace/zoneapi/tests/SignupTest.php:22

I tried to use the seeJsonStructure() method but I got an undefined method error. See here:

PHPUnit 4.8.23 by Sebastian Bergmann and contributors.



  [Symfony\Component\Debug\Exception\FatalErrorException]  
  Call to undefined method SignupTest::seeJsonStructure()  



Fatal error: Call to undefined method SignupTest::seeJsonStructure() in /home/ubuntu/workspace/zoneapi/tests/SignupTest.php on line 22

Call Stack:
    0.0002     237272   1. {main}() /home/ubuntu/workspace/zoneapi/vendor/phpunit/phpunit/phpunit:0
    0.0214    1801432   2. PHPUnit_TextUI_Command::main() /home/ubuntu/workspace/zoneapi/vendor/phpunit/phpunit/phpunit:47
    0.0214    1802056   3. PHPUnit_TextUI_Command->run() /home/ubuntu/workspace/zoneapi/vendor/phpunit/phpunit/src/TextUI/Command.php:100
    0.3897    6474744   4. PHPUnit_TextUI_TestRunner->doRun() /home/ubuntu/workspace/zoneapi/vendor/phpunit/phpunit/src/TextUI/Command.php:149
    0.4090    6694592   5. PHPUnit_Framework_TestSuite->run() /home/ubuntu/workspace/zoneapi/vendor/phpunit/phpunit/src/TextUI/TestRunner.php:440
    0.4095    6710712   6. PHPUnit_Framework_TestSuite->run() /home/ubuntu/workspace/zoneapi/vendor/phpunit/phpunit/src/Framework/TestSuite.php:747
    0.4227    6714992   7. PHPUnit_Framework_TestCase->run() /home/ubuntu/workspace/zoneapi/vendor/phpunit/phpunit/src/Framework/TestSuite.php:747
    0.4228    6716632   8. PHPUnit_Framework_TestResult->run() /home/ubuntu/workspace/zoneapi/vendor/phpunit/phpunit/src/Framework/TestCase.php:724
    0.4245    6784448   9. PHPUnit_Framework_TestCase->runBare() /home/ubuntu/workspace/zoneapi/vendor/phpunit/phpunit/src/Framework/TestResult.php:612
    0.6029   14322288  10. PHPUnit_Framework_TestCase->runTest() /home/ubuntu/workspace/zoneapi/vendor/phpunit/phpunit/src/Framework/TestCase.php:768
    0.6030   14323048  11. ReflectionMethod->invokeArgs() /home/ubuntu/workspace/zoneapi/vendor/phpunit/phpunit/src/Framework/TestCase.php:909
    0.6030   14323128  12. SignupTest->testSignup() /home/ubuntu/workspace/zoneapi/vendor/phpunit/phpunit/src/Framework/TestCase.php:909

Laravel test documentation

Aucun commentaire:

Enregistrer un commentaire