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?
This worked for me finally:
$I->seeResponseContainsJson(['data' => [
'first_name' => 'Test'
]]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With