user2094178
user2094178

Reputation: 9454

Codeception, unable to simulate ajax behavior

I can't replicate ajax calls via codeception.

For example:

$I->sendAjaxPostRequest('login/verify', array('name' => 'name', 'password' => 'password'));
$I->seeResponseIsJson();

Will not raise any errors. But in the other hand, if I do the following:

$I->sendAjaxPostRequest('login/verify', array('name' => 'name', 'password' => 'password'));
$I->seeResponseIsJson();
$I->seeResponseContainsJson(['login_failed' => 1]);
//or
$I->grabDataFromJsonResponse('data.login_failed');

It gives me this error:

ErrorException: Argument 2 passed to Codeception\Module\REST::arrayHasArray() must be of the type array, null given, called in C:\xampp\htdocs\blog\laravel\vendor\codeception\codeception\src\Codeception\Module\REST.php on line 485 and defined

What I understand from the error above is that seeResponseContainsJson or grabDataFromJsonResponse internally will pass a response as a second argument to arrayHasArray. But it looks like no matter what the response is always empty.

Also, if I do the following:

$I->sendAjaxPostRequest('login/verify', array('name' => 'name', 'password' => 'password'));
var_dump($I->grabResponse());

I receive this for var_dump():

object(Codeception\Maybe)#753 (3) {
  ["position":protected]=>
  int(0)
  ["val":protected]=>
  NULL
  ["assocArray":protected]=>
  NULL
}

Everything else works as expected with Codeception, I'm using PhpBrowser.

Upvotes: 5

Views: 2051

Answers (1)

ebonhand
ebonhand

Reputation: 346

I'm sure not how useful this answer is to anyone else, but I landed here whilst googling for a similar error message:

ErrorException: Argument 2 passed to Codeception\Module\REST::arrayHasArray() 
must be of the type array, null given

After much hair-pulling, I discovered that some debug output from my controller (a var_dump) was causing the returned document to not be valid JSON, and using $I->seeResponseContainsJson() was therefore throwing errors internally, as the response wasn't valid JSON

So, ensure the response your controller is sending is valid JSON, and this error should go away

Upvotes: 1

Related Questions