Friday, October 31, 2008

Re: Test Suite and HABTM Models - Not Working

Thanks Anupom,

That got me past the missing table cake errors, but now 2/3 of the
tests that cake generated are failing.

Here is my post.test.php

App::import('Model', 'Post');


class PostTestCase extends CakeTestCase {
var $Post = null;
var $fixtures = array('app.post');

function start() {
parent::start();
$this->Post = & ClassRegistry::init('Post');
}

function testPostInstance() {
$this->assertTrue(is_a($this->Post, 'Post'));
}

function testPostFind() {
$results = $this->Post->recursive = -1;
$results = $this->Post->find('first');
$this->assertTrue(!empty($results));

$expected = array('Post' => array(
'id' => 1,
'name' => 'Lorem ipsum dolor sit amet'
));
$this->assertEqual($results, $expected);
}
}

The two cases that fail both reference testPostFind(). After a little
poking around, $this->Post->find('first') does not return anything at
all. The sql queries insert my data from the fixture and truncate it 3
times before finally dropping the tables. However, the query that I
believe is being generated from find('first') is being run on my
default connection, as opposed to the test connection. Shouldn't this
query be run on the test database since that's where the dummy data is
being inserted?

Any help is appreciated.

On Oct 31, 12:48 am, Anupom <anupom....@gmail.com> wrote:
> Hi Mike,
>
> I think the RC3 bake script still generates old style code, like the
> following -
>
> App::import('Model', 'Post');
>
> class TestPost extends Post {
>     var $cacheSources = false;
>     var $useDbConfig  = 'test_suite';
>
> }
>
> class PostTestCase extends CakeTestCase {
>     var $Post = null;
>     var $fixtures = array('app.post', 'app.tag');
>
>     function start() {
>         parent::start();
>         $this->Post = new TestPost();
>     }
> .....
> .....
> .....
>
> Unfortunately this old style is redundant and does not work with
> associations (without some hacks). But GOOD news is that - Tim and Nate have
> come up with an even better solution and that works with associations
> flawlessly. Edit your test files and make it like the following - it's gonna
> work like a charm.
>
> App::import('Model', 'Post');
>
> class PostTestCase extends CakeTestCase {
>     var $Post = null;
>     var $fixtures = array('app.post', 'app.tag');
>
>     function start() {
>         parent::start();
>         $this->Post =& ClassRegistry::init('Post');
>     }
> .....
> .....
> .....
>
> Read Tim's blog on this for more info.http://debuggable.com/posts/testing-models-in-cakephp---now-let%27s-g...
>
>
>
> On Fri, Oct 31, 2008 at 3:18 AM, MikeB <bern...@gmail.com> wrote:
>
> > I'm having a problem with the Cake Test Suite throwing errors about
> > HABTM join tables not being found. I've successfully recreated the
> > problem on a much smaller scale.
>
> > I've created a very basic 3 table database:
>
> > posts (id, name)
> > posts_tags (post_id, tag_id)
> > tags (id, name)
>
> > I've baked each model without incident. When I go to test.php and try
> > to run the post.test.php test that was generated for me, it gives me
> > this error:
>
> > Error:  Database table posts_tags for model PostsTag was not found.
>
> > I'm using a default AND test db connection.
> > Cake 1.2 rc3
> > CakePHP Test Suite v 1.2.0.0
> > SimpleTest v1.0.1
>
> > Keep in mind that I have not touched any of these files. They are all
> > freshly baked out the oven.
>
> > Any ideas?
>
> --
> Anupom Syamhttp://syamantics.com/
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

No comments: