如何在CakePHP2.0中通过testAction传递参数

如何在CakePHP2.0中通过testAction传递参数,cakephp,testing,parameters,phpunit,cakephp-2.0,Cakephp,Testing,Parameters,Phpunit,Cakephp 2.0,我想用这个标题测试一个函数: public function includeNumComments($posts){ 其中$post是一个数据数组 我想知道如何通过一系列帖子来测试这个方法 我尝试过这样的方法,但不起作用: $result = $this->testAction("/comments/includeNumComments/", array('data' => $posts)); $result = $this->testAction("/comments/i

我想用这个标题测试一个函数:

public function includeNumComments($posts){
其中$post是一个数据数组

我想知道如何通过一系列帖子来测试这个方法

我尝试过这样的方法,但不起作用:

$result = $this->testAction("/comments/includeNumComments/", array('data' => $posts));

$result = $this->testAction("/comments/includeNumComments/", array($posts));

谢谢这并没有真正使用
testAction
,因为您无法通过HTTP传递数组。没有办法通过网站上的表格或链接做到这一点

您可以将其作为正常功能进行测试:

$result = $this->CommentsController->includeNumComments($posts);

这并不是真正使用
testAction
,因为您不能通过HTTP传递数组。没有办法通过网站上的表格或链接做到这一点

您可以将其作为正常功能进行测试:

$result = $this->CommentsController->includeNumComments($posts);

这是正确的案例,它对我有效。希望有帮助:

public function testAddUser() {
    $data = array(
        'User' => array(
            'id' => 12,
            'username' => 'testname1',
            'password' => 'Pass@123!',
            'email' => 'test@example.com'
        )
    );
    $result = $this->testAction(
        '/users/add',
        array('data' => $data, 'method' => 'post')
    );
    debug($result);

}

这是正确的案例,它对我有效。希望有帮助:

public function testAddUser() {
    $data = array(
        'User' => array(
            'id' => 12,
            'username' => 'testname1',
            'password' => 'Pass@123!',
            'email' => 'test@example.com'
        )
    );
    $result = $this->testAction(
        '/users/add',
        array('data' => $data, 'method' => 'post')
    );
    debug($result);

}

好的,谢谢。然后。。。我无法以“vars”、“contents”或“view”的形式获取返回的数据?我的观点是,该操作不能作为常规的控制器操作开始,因为您永远无法传递数组。如果您想让它作为一个控制器动作工作,您需要重构它以不接受数组。如果你在现实生活中尝试它,你无法通过数组,所以它无论如何都会失败。所以。。。任何控制器上的函数都不能将数组作为参数?如果要通过HTTP(url)访问它,则不能。如果它是一个不能作为操作访问的帮助器方法(例如
beforeFilter
),那么yeah go it.yeah it’s ok。这个URL不可访问。好的,谢谢。然后。。。我无法以“vars”、“contents”或“view”的形式获取返回的数据?我的观点是,该操作不能作为常规的控制器操作开始,因为您永远无法传递数组。如果您想让它作为一个控制器动作工作,您需要重构它以不接受数组。如果你在现实生活中尝试它,你无法通过数组,所以它无论如何都会失败。所以。。。任何控制器上的函数都不能将数组作为参数?如果要通过HTTP(url)访问它,则不能。如果它是一个不能作为操作访问的帮助器方法(例如
beforeFilter
),那么yeah go it.yeah it’s ok。此URL不可访问。