Christophegonfrere
Christophegonfrere

Reputation: 39

test api controller that returns json response

In order to test an API controller in a Symfony2 project that returns json response, I tried to generate a route of the action like here:

$client->getContainer()->get('router')->generate('/api/register/emailverification/', array('email' => '[email protected]'), true)
$response= $client->getResponse();
$this->assertEquals(200, $response);`

But the response returns null. I don't know if have to do a specific test for this type of response like using guzzle...

Upvotes: 1

Views: 905

Answers (2)

COil
COil

Reputation: 7606

In tests one shouldn't "generate" routes paths, they must be hard-coded:

$client = $this->createClient();
$client->request('GET', '/api/register/emailverification/[email protected]');
$this->assertTrue($client->getResponse()->isOk());

But if you want to test json, you can do:

$this->assertJson($client->getResponse()->getContent());

You can find additional PHPunit helpers in the rest extra bundle.

Upvotes: 2

Antonis Charalambous
Antonis Charalambous

Reputation: 1062

I believe you should try:

$controllerResponse = $client->getContainer()->get('router')->generate('_validation_email', array('email' => '[email protected]'), true)
$response= $controllerResponse->getResponse();
$this->assertEquals(200, $response);

Upvotes: 0

Related Questions