Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 847
user2094178 Avatar asked Nov 13 '22 01:11

user2094178


1 Answers

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

like image 145
ebonhand Avatar answered Nov 15 '22 10:11

ebonhand