Memory database during unit testing ?

I’m a new Yii developper and have one question about unit tests.

Can I use a memory DB during unit testing ?

I’m from a java framework (Play! Framework) and with it I can use a different database for production and for tests.

Is it possible with Yii ??

Tanks.

Yes it is possible.

You can define your Test DB in protected/config/main.php.

for example Mysql




'db' => arry(

'connectionString' => 'mysql:host=localhost;dbname=YourDBname',

'emulatePrepare'   => true, // needed by some MySQL installations

'username'         => 'YourUser',

'password'         => 'YourPassword',

'charset'          => 'utf8',

)



and for more information about DB you can find here

sorry for my bad English

You can use a dedicated configuration file for tests. Take a look here.

Yes, you can. See here: http://code.google.com/p/yii/source/browse/trunk/tests/framework/db/ar/CActiveRecordTest.php

Thanks, I use now the test.php file in the config folder.

I specified :


'db'=>array(

           'connectionString' => 'sqlite:'.dirname(__FILE__).'/../data/testdrive.db',

 ),

Tests don’t go in my mysql database now but this database doesn’t contain my models tables.

So I have this error :

CDbException: The table "User" for active record class "User" cannot be found in the database.

Any idea ?

You should create table first.

Do not support table prefix, it will throw a exception that can not found table name "{{xxx}}" doing this way.

MySQL has a dedicated table type called MEMORY for in-memory tables.

Regards,

The SQLite in-memory option seems to be best. At least that’s what the creator of PHPUnit recommends.