How to choose the test DB cakePHP testing

2019-06-24 14:55发布

问题:

I am using the cake bake command to create my fixture and Test models. I have done it successfully with one of my models but i am having troubles with another one.

I dont know why, it tries to work with the default DB instead with the test one that i have already defined and that is the one used by the first model i tested.

Both Test models, the one which works well and this 2nd one which doesn't, have been created with cake bake and they look exactly the same.

Both of them have this in their fixture class:

public $import = array('records' => true, 'connection' => 'test');

Test defines the connection to my test database. What can be the problem?

I have experiment something weird. In my test model if i use the singluar instead of the plural, it works with the default DB BUT if i change it to plural it works with the test DataBase. (but it can not call any method from the model, just defined methods such as find()).

For example:

public function setUp() {
    parent::setUp();

    //uses the default DB (i dont know why)
    $this->Post = ClassRegistry::init('Post');          
    print_r($this->User->find('all'));

    $this->Post->updatePostsStatus(1); //works well

    //uses the default Test Database (i dont know why either)
    $this->Post = ClassRegistry::init('Posts');         
    print_r($this->User->find('all'));   

    $this->Post->updatePostsStatus(1); //doesn't work. No function found.
    /*SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'updatePostsStatus' at line 1*/

}

Thanks.

I HAVE UPDATED IT.

LAST UPDATE AND SOLUTION

The problem was the bad initialization of the Post Model constructor. Using the plural was not relevant. It is like using any non existent model. It will always get the test database. The answer for the good initialization of the constructor is here: How to override model's constructor correctly in CakePHP

回答1:

That sort of MySQL error can be very misleading in CakePHP.

Basically the actual problem is with the line $this->Post->updatePostsStatus(1); Specifically it's that eitherthe Post model or updatePostsStatus() method of the posts model doesn't exist.

You basically have a typo on this line, check the method name. Should it be updatePostStatus? (no plural on posts?)


For some reason, if you try to call an undefined method on a model in CakePHP, instead of reporting the method is not found, the "automagicness" of CakePHP kicks in and bizarrely it tries to push the unfound method name straight into MySQL.

Basically, because CakePHP cannot find the $this->Post->updatePostsStatus(1); method, it is actually trying to execute a MySQL query of updatePostsStatus ie mysql_query("updatePostsStatus');, which is why we are seeing this wierd database error. Chances are you've just misspelled the method name.


If the method name is still not the problem, it's something to do with the Posts model not being instantiated correctly. Although that seems doubtful as long as you got all the names right. It is definitely not a database issue, just Cakes lame way of handling unknown class methods automagically.