Testing models
Let's say we already have our Article model defined on app/models/article.php, which looks like this:
Простой текст<?phpclass Article extends AppModel {var $name = 'Article';function published($fields = null) {$conditions = array($this->name . '.published' => 1);return $this->findAll($conditions, $fields);}}?>
We now want to set up a test that will use this model definition, but through fixtures, to test some functionality in the model. CakePHP test suite loads a very minimum set of files (to keep tests isolated), so we have to start by loading our parent model (in this case the Article model which we already defined), and then inform the test suite that we want to test this model by specifying which DB configuration it should use. CakePHP test suite enables a DB configuration named test_suite that is used for all models that rely on fixtures. Setting $useDbConfig to this configuration will let CakePHP know that this model uses the test suite database connection.
Since we also want to reuse all our existing model code we will create a test model that will extend from Article, set $useDbConfig and $name appropiately. Let's now create a file named article.test.php in your app/tests/cases/models directory, with the following contents:
<?phpApp::import('Model','Article');class ArticleTestCase extends CakeTestCase {var $fixtures = array( 'app.article' );}?>
We have created the ArticleTestCase. In variable $fixtures we define the set of fixtures that we'll use.
If your model is associated with other models, you will need to include ALL the fixtures for each associated model even if you don't use them. For example: A hasMany B hasMany C hasMany D. In ATestCase you will have to include fixtures for a, b, c and d.


Коментарии:
Добавить коментарий