2013-05-01 12 views
5

Perché non riesco ad ottenere l'input e testare se non è nullo?Test PHPUnit per uscita JSON

Il mio metodo sto testando:

/** 
* @method: getCategory 
* retrieves the categories 
* @return json category data 
*/ 
public function getCategory() { 

    $cat = $this->em->getRepository('Entities\Category')->findAll(); 
    $data = array(); 
    foreach ($cat as $res) { 
     $data[] = array(
      'catId' => $res->__get('catId'), 
      'category' => $res->__get('category'), 
      'item' => $res->__get('item') 
     ); 
    } 
    echo json_encode($data); 
} 

La mia prova:

/** 
* @covers Category::getCategory 
* @todo Implement testGetCategory(). 
*/ 
public function testGetCategory() { 
    $json = $this->object->getCategory(); 
    $this->assertNotNull($json); 
} 

Il messaggio di errore, restituisce un array di oggetti JSON di:

PHPUnit 3.7.8 by Sebastian Bergmann.

F[{"catId":1,"category":"FLORALS2","item":"RED ROSES"}, {"catId":2,"category":"TENTS","item":"12X14"}, {"catId":3,"category":"FLORAL","item":"WHITE ROSES"}, {"catId":4,"category":"TENTS","item":"15X24"}, {"catId":5,"category":"CHAIRS","item":"BLACK CHAIR"}, {"catId":6,"category":"CHAIRS","item":"RED CHAIRS"}, {"catId":7,"category":"TENTS","item":"23X23"}, {"catId":8,"category":"CANDLES","item":"RED CANDLES"}, {"catId":9,"category":"CANDLES","item":"WHITE CANDLES"}, {"catId":10,"category":"CANDLES","item":"BLACK CANDLES"}, {"catId":11,"category":"CANDLES","item":"ORANGE CANDLES"}, {"catId":12,"category":"TABLE","item":"4X8 TABLE"}, {"catId":13,"category":"DRAPERYS","item":"24\" WHITE LINEN"}, {"catId":14,"category":"LINEN","item":"WHITE CURTAINS"}, {"catId":17,"category":"DRAPERY","item":"SILK TABLE CLOTH"}, {"catId":18,"category":"FLORAL","item":"ORANGE DAISIES"}]..

Tempo: 0 secondi, Memoria: 10.25Mb

c'era 1 fallimento:

1) CategoryTest::testGetCategory Failed affermando che null non è nullo. Funzione

/var/www/praiseDB/tests/controller/CategoryTest.php:42

+0

L'assertXXXX è sostanzialmente l'opposto di quello che stai pensando. Ad esempio, l'assertNotNull lancia l'asserzione quando il testo è Null. Il tuo $ json è NULL poiché era echo, non restituito. –

risposta

5

tuo getCategory() fa eco qualcosa:

echo json_encode($data); 

Ma non restituisce nulla. Quindi la variabile $ json sarà nulla nel test.

Probabilmente significava per restituire il valore al termine della funzione, invece:

return json_encode($data); 

Per la prova di uscita si avrebbe bisogno di utilizzare i metodi expectOutputString() o expectOutputRegex() nel test. Per eseguire il test per l'uscita non vuoto, credo che il seguente dovrebbe fare:

/** 
* @covers Category::getCategory 
* @todo Implement testGetCategory(). 
*/ 
public function testGetCategory() { 
    $this->expectOutputRegex('/./'); 
    $this->object->getCategory(); 
} 

Vedere la phpunit documentation per i dettagli su come far valere uscita.

+0

Sì, questo è il modo in cui dovrebbe tornare. Ho una chiamata Ajax che ottiene la richiesta. Apparentemente non riesco a testarlo in questo modo, quindi come faccio a testare che il metodo echi qualcosa? – robasc

+0

Hai dimenticato di dire che questo è un metodo di controllo. Poiché si tratta dell'output di ritorno, è necessario scrivere un test per verificare che il metodo del controller restituisca l'output, ma non riesco a trovare esempi o tutorial validi. – robasc

+0

ok, quindi l'eco era davvero quello che volevi, ho modificato la mia risposta per aggiungere alcuni dettagli su come affermare l'output con phpunit – Thierry