Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to seeResponseContainsJson two levels deep with Codeception?

I am new to Codeception and I am trying to test my web service using the same. Right now I am trying to figure out how to dig deep and test the output that comes from different API points. For example, I am trying to create a user and check if the response contains the necessary details.

CreateUserCept.php

<?php 
$faker = Faker\Factory::create();
$I = new ApiTester($scenario);
$I->wantTo('create a new user');
$I->haveHttpHeader('Authorization', 'Bearer ' . file_get_contents('tests/api/token'));
$I->sendPost('users', [
    "first_name"    => "Test",
    "last_name"     => "Test",
    "email"         => '[email protected]',
    "password"      => "testing",
    "role"          => "1"
]);
$I->seeResponseCodeIs(200);
$I->seeResponseIsJson();

The response looks something like:

{
  "status": "success",
  "data": {
    "first_name": "Test",
    "last_name": "Test",
    "email": "[email protected]",
    "updated_at": "2015-11-12 09:08:31",
    "created_at": "2015-11-12 09:08:31",
    "id": 54
  },
  "errors": null,
  "message": "Resource Created Successfully"
}

Now I can do assertions like:

$I->seeResponseContainsJson(['status' => 'success']);

And it works like a charm but when I do this:

$I->seeResponseContainsJson(['data.first_name' => 'Test']);

It fails. What is the correct way to dig into these and check the JSON response is correct?

like image 298
Rohan Avatar asked Oct 23 '25 15:10

Rohan


1 Answers

This worked for me finally:

$I->seeResponseContainsJson(['data' => [
    'first_name' => 'Test'
]]);
like image 56
Rohan Avatar answered Oct 26 '25 06:10

Rohan