Everything I Know About Getting Model Tests to Work Half-Way Rationally in CakePhp
My Sources
http://github.com/mcurry/cakephp/tree/master/test_sample/testshttp://debuggable.com/posts/testing-models-in-cakephp---now-let's-get-rid-of-the-unnecessary-modeltest-classes-!:4890ed55-be28-4d4a-ba4c-7fd64834cda3#comment-492aedab-164c-421c-a1a8-4d854834cda3
Some Caveats
- It's possible that this will not work depending on which release of CakePhp we have installed. Testing seems to be a part of the codebase that is still in a bit of flux.
- You'll want to set up a second empty database. At home I have two databases set up for model testing:
db1-fixture (empty database -- CakePhp creates tables here for test and truncates at end)
- You need to have fixtures set up for the model you're testing and all associated models. Fortunately, the fixtures are very simple.
- Add a 'test_suite' option to config/database.php and set the db to your new empty database (e.g. db1-fixture).
- If you run into db table not found errors, make sure your fixture db is completely empty. If it has tables in it at the start of test, I would get this error for some reason. Just truncate the db.
- Start with a simple example from your project based on the sample code and build from there.
Simple Examples
<?php
class ModelFixture extends CakeTestFixture
{
var $name = 'Model';
var $import = array('table' => 'models', 'import' => false);
var $useDbConfig = 'test_suite';
var $records = array(
array(
'id' => 1,
'etc' => 0,
)
);
}
?>
class ModelFixture extends CakeTestFixture
{
var $name = 'Model';
var $import = array('table' => 'models', 'import' => false);
var $useDbConfig = 'test_suite';
var $records = array(
array(
'id' => 1,
'etc' => 0,
)
);
}
?>
<?php
/*
Based on mcurry's template at
http://github.com/mcurry/cakephp/tree/master/test_sample/tests
*/
class ExtagProofCase extends CakeTestCase {
var $Model = null;
var $fixtures = array(
'app.model',
'app.model1',
'app.model2',
);
function start()
{
parent::start();
$this->Model = ClassRegistry::init('Model');
}
function testInstance() {
$this->assertTrue(is_a($this->Model, 'Model'));
}
}
?>
/*
Based on mcurry's template at
http://github.com/mcurry/cakephp/tree/master/test_sample/tests
*/
class ExtagProofCase extends CakeTestCase {
var $Model = null;
var $fixtures = array(
'app.model',
'app.model1',
'app.model2',
);
function start()
{
parent::start();
$this->Model = ClassRegistry::init('Model');
}
function testInstance() {
$this->assertTrue(is_a($this->Model, 'Model'));
}
}
?>
There are no comments on this page. [Add comment]