简体   繁体   English

控制器单元测试失败,并显示SQLSTATE [42000]错误

[英]Controller Unit Test fails with SQLSTATE[42000] error

I want to unit test a controller action and have some problem toio excecute it. 我想对控制器动作进行单元测试,并且有一些问题要执行。 The Error i got is the following: 我得到的错误如下:

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; SQLSTATE [42000]:语法错误或访问冲突:1064 SQL语法有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near 'questionExists' at line 1 检查与您的MySQL服务器版本相对应的手册以获取正确的语法,以在第1行的“ questionExists”附近使用

The Method: 方法:

questionExists 问题存在

is defined inside the Question Model. 在问题模型中定义。

My test function looks like this: 我的测试功能如下所示:

public function testView() {
        $result = $this->testAction('/questions/questions/view/1', array('return' => 'vars'));
    }

The Controller action i want to test looks like this: 我要测试的Controller动作如下所示:

public function view($id = null) { 公共功能视图($ id = null){

    if (!$this->Question->questionExists($id, 'id_virtual')) {
        throw new NotFoundException(__('Invalid question'));
    }
    $options = array('conditions' => array('Question.id_virtual' => $id));
    $this->set('question', $this->Question->find('first', $options));
}

So this is very confusing to me. 所以这让我很困惑。 Can anybody point me to the right direction ? 有人能指出我正确的方向吗?

Your model class is not found, CakePHP creates an instance on the fly for that table but this is not more than the basic Model class. 找不到您的模型类,CakePHP会为该表动态创建一个实例,但这只不过是基本的Model类。 When the code is then called it tries to call that method on an instance that is not your Question model in your app. 然后调用代码时,它将尝试在不是您应用程序中的Question模型的实例上调用该方法。 You'll have to figure out what that happens. 您必须弄清楚会发生什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM