2017-09-18 2 views
0

apprendre Lumen v5.5 php framework et avoir atteint un barrage routier créant mon premier test unitaire php. L'application fonctionne en utilisant Postman si je définis l'URL et sélectionnez la méthode POST et dans le corps, choisissez "raw" et réglez le type sur "application/json". Donc, nous connaissons déjà le post-travail, cherchant simplement à créer un test.Lumen 5.5 unité-test post json

est ici la chose la plus proche de ce que je pense un test de l'unité de travail doit être:

public function testPostJson() { 
    $the_json = '{"client_guid": "C00A0EA5-3F64-01EA-C4B6-159EA145AB3B"}'; 
    $the_headers = [ 'CONTENT_TYPE' => 'application/json' ]; 
    $this->call(
     'POST', 
     '/getClientNotes', 
     [], 
     [], 
     [], 
     $the_headers, 
     $the_json 
    ); 
    $this->assertResponseOk(); 
    $this->assertEquals(true, $this->response->status); 
} 

Mais bien sûr, il renvoie des erreurs comme:

  • 1) NotesTest :: testPostJson code d'état attendu 200, a obtenu 404. *
  • 2) ErrorException: Undefined property: Illuminate \ Http \ Response :: $ status

Pas grand-chose de la manière de la documentation, même à la lumière site docs: https://lumen.laravel.com/docs/5.5/testing

+0

est l'itinéraire créé pour POST ou GET? getClientNotes ressemble à une requête GET. – xelber

+0

non, pas un get son POST ici est la route: $ app-> post ('note/getClientNotes', 'NoteController @ getClientNotes'); –

+0

bien merde, il y a ma première erreur ... '/ getClientNotes', devrait être 'note'/getClientNotes ', –

Répondre

0

est bien ici le code de travail ... Je manque la « note/» dans le 2ème paramètre du $ this-> appel().

class NotesTest extends TestCase { 

protected $send_headers = null; 
protected $send_json = null; 
protected $receive_json = null; 

public function testIsApiAlive() { 
    $this->call("GET",'/'); 
    $this->assertEquals($this->app->version(), $this->response->getContent()); 
    $this->assertEquals(200, $this->response->status()); 
} 

public function testPostJson() { 
    $this->send_json = '{"client_guid": "C00A0EA5-3F64-01EA-C4B6-159EA145AB3B"}'; 
    $this->send_headers = [ 'CONTENT_TYPE' => 'application/json' ]; 
    $this->call(
     'POST', 
     'note/getClientNotes', 
     [], 
     [], 
     [], 
     $this->send_headers, 
     $this->send_json 
    ); 
    $this->receive_json = json_decode($this->response->getContent()); 
    $this->assertEquals(200, $this->response->status()); 
    $this->assertTrue(isset($this->receive_json->status)); 
    $this->assertTrue($this->receive_json->status == true); 
    $this->assertTrue(isset($this->receive_json->data[0]->agency)); 
    $this->assertTrue($this->receive_json->data[0]->agency == 'demo'); 
} 

Voici les résultats de la ligne de commande:

C:\xampp\htdocs\jsonproject\tests\>phpunit NotesTest.php 
PHPUnit 5.7.21 by Sebastian Bergmann and contributors. 

..                 2/2 (100%) 

Time: 740 ms, Memory: 10.00MB 

OK (2 tests, 7 assertions)