2013-07-14 24 views
5

我无法通过代码复制ajax调用。Codeception,无法模拟ajax行为

例如:

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

不会引发任何错误。但在另一方面,如果我做到以下几点:

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

它给我这个错误:

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

上面我从错误理解的是,seeResponseContainsJson或grabDataFromJsonResponse内部将通过作为响应arrayHasArray的第二个参数。但看起来不管响应总是空的。

另外,如果我做到以下几点:

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

我收到此为的var_dump():

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

其他一切工作与Codeception预期,我使用PhpBrowser。

回答

1

我敢肯定,这个答案并不怎么有用的是别人,但我在这里登陆,而google搜索类似的错误消息:

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

后多头发拉扯,我发现了一些调试输出我的控制器(一个var_dump)导致返回的文档不是有效的JSON,并且因此使用$I->seeResponseContainsJson()因此在内部抛出错误,因为响应无效JSON

因此,确保控制器发送的响应是有效的JSON ,并且此错误应该消失