views:

195

answers:

0

Hey all,

So i'm working with CakePHP v1.2.5. On my current project, I decided to start writing tests as I code the functionality (yay TDD). I'm having trouble with fixture loading though.

To aid in the process, I'll describe my code (Really quite simple right now). My model is defined like so

// app/models/newsitem.php
<?php
class NewsItem extends AppModel
{
  var $name='NewsItem';
}
?>

// app/tests/fixtures/newsitem_fixture.php
<?php

class NewsItemFixture extends CakeTestFixture 
{
    var $name = 'NewsItem';
    var $import = 'NewsItem';

    var $records = array(
     array('id' => '1', 'title' => 'News Item 1', 'body' => 'This is the first piece of news', 'created' => '2007-03-18 10:39:23', 'modified' => '2007-03-18 10:41:31'),
     array('id' => '2', 'title' => 'News 2', 'body' => 'This is some other piece of news', 'created' => '2009-05-04 9:00:00', 'modified' => '2009-05-05 12:34:56')
    );
}

?>

// app/tests/models/newsitem.test.php
<?php
App::Import('Model', 'NewsItem');

class NewsItemTestCase extends CakeTestCase
{
    var $fixtures = array('app.newsitem');

    function setUp()
    {
     $this->NewsItem =& ClassRegistry::init('NewsItem');
    }

    function testFindAll()
    {
     $results = $this->NewsItem->findAll();
     $expected = array(
      array('NewsItem' => array('id' => '1', 'title' => 'News Item 1', 'body' => 'This is the first piece of news', 'created' => '2007-03-18 10:39:23', 'modified' => '2007-03-18 10:41:31')),
      array('NewsItem' => array('id' => '2', 'title' => 'News 2', 'body' => 'This is some other piece of news', 'created' => '2009-05-04 9:00:00', 'modified' => '2009-05-05 12:34:56'))
     );
     print_r($results);
     $this->assertEqual($results, $expected);
    } 
}

?>

Anyway, my problem is, when I run the test suite in a browser (going to http://localhost/test.php), the test case runner tries to load my app's layout (which is weird cuz I'm just testing the model) which references another model which is obviously not loaded in the test database and I get an error.

And if I remove the var $fixtures = array('app.newsitem') line from my NewsItemTestCase file, the test case runs properly, BUT it doesn't load the fixtures (for obvious reasons).

Any ideas, suggestions? To be honest I'm having a little trouble finding more than 3 tutorials on this matter.