cakephp - 使用数组测试 Controller Cakephp 2 方法

标签 cakephp testing phpunit cakephp-2.1

嗨,我在 Controller 中有这个方法

 public function registeruser($data){
    var_dump($data);
    if(($data) && ($this->User->create($data))) {
      if($data['User']['password'] != Security::hash($data['User']['confirm_password'], null, true) ){
        $this->Session->setFlash('las contraseñas no coinciden');
        // unset($this->data['User']['password']);
        // unset($this->data['User']['confirm_password']);
        $this->redirect('index');
      }
      else if($this->User->save($data)) {
         $this->_subscribMailchimp($data);

      }else {
        $this->Session->setFlash('El mail no es valido o ya está registrado.');
        unset($data['User']['password']);
        unset($data['User']['confirm_password']);
        $this->redirect('index');
      }
    }else {
      $this->Session->setFlash('No se ha podido registrar.');
      $this->redirect('index');
    }
    return $this->User->getLastInsertID();
}

我需要通过 $data 表单测试 Controller

    $data = array('User' => array('name' => 'Luis jose',
                                  'address' => 'el quinto pino',
                                  'zip' => '28012',
                                  'city' => 'Madrid',
                                  'province' => '5',
                                  'email' => 'luis@hotmail.com',
                                  'password' => Security::hash(Configure::read('Security.salt') . '12345'),
                                  'confirm_password' => '12345'));
    $this->testAction('/users/registeruser', array('data' => $data, 'method' => 'get'));

我试过这个数组('data' => $data, 'method' => 'get')。 Controller 函数没有接收到数组

最佳答案

您的注册用户需要 1 个参数($data),因此 URL 应如下所示:

/foo/registeruser/bar

这会用'bar'填充$data

您应该尝试 debug($this->request) 以查看使用 GET 或更好的方式传递数据的结果,使用 POST 并查看 $this->request->data (CakePHP 2) 或 $this->数据(CakePHP 1.3)

关于cakephp - 使用数组测试 Controller Cakephp 2 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10948704/

相关文章:

cakephp - 在CakePHP中创建全局变量的最佳方法是什么?

cakephp - CakePHP 3 中的子查询?

reactjs - 如何在 puppeteer 中模拟拖放 Action ?

Spring mvc junit 测试服务

php - 自动化测试生成?

php - CakePHP 组和计数

javascript - Cakephp 图像未显示在 view.ctp 中

testing - Paypal 现场测试

symfony - 使用 Phpunit : InvalidArgumentException: Unreachable field 进行功能测试

php - 公共(public)、私有(private)或 protected 属性(property)?