Friday, November 28, 2008

Re: CakePHP Unit Test Model Woes :(

Dave

I think I had (have) the same problem. Don't ask me why, but try something like this. I had to force useDbConfig in certain circumstances. (I originally posted this on the blog at debuggable)

App::Import('Model', 'Cutoff');

class CutoffTestCase extends CakeTestCase {

var $fixtures = array('app.cutoff');

function start() {
parent::start();
$this->Sut = & ClassRegistry::init('Cutoff');
$this->Sut->useDbConfig = 'test_suite';
}

function testXYZ() {
// Findings will vary depending on whether useDbConfig is set above.
// If useDbConfig is omitted - get results from default (live) DB
// If useDbConfig is set to test_suite, get results from $test DB config
pr($this->Sut->find('all'));
// assertSomething down here
}

}


Howard


On Fri, Nov 28, 2008 at 5:34 AM, Dave C <dmcassel@gmail.com> wrote:

Stop me if you've heard this before, but I'm having a problem unit
testing models. I've followed this conversation and poked around a
bunch elsewhere. Progress, but not yet a solution.

Problem: When I run my test case, CakePHP successfully inserts my
fixture data in to "user_tests" (and associated tables). But when it
runs the query, it selects from "users". This is using CakePHP 1.2
RC3.

The output shows me that it is working with the test_suite database
connection. I'm starting the test database with the tables defined,
but empty. After calling ClassRegistery::init(), $this->User->table is
'users', as is $this->User->useTable. I'd expected to see 'user_tests'
for it to work, but no dice.

Can anybody tell me what I'm doing wrong?

Thanks,

Dave C.

// condensed model (leaving out an association and other methods
class User extends AppModel {
   function validateLogin($data) {
       $params = array('conditions' => array('username' => $data
['username'],
                                             'passwd' =>sha1($data
['passwd'])),
                       'recursive' => 2,
                       'fields' => array('id', 'username'));
       $user = $this->find('all', $params);
       ...
   }
}

// my fixture:
class UserTestFixture extends CakeTestFixture {
   var $name = 'UserTest';

   public $import = 'User';

   public $records = array(
       array('id' => 1, 'name' => 'Test User1', 'username' =>
'test1',
             'email' => 'a1@b.c', 'parentuser_id' => null)
   );
}

// my test:
class UserTestCase extends CakeTestCase {
   public $fixtures = array('app.user_test');

   public function testValidateLogin() {
       $this->User =& ClassRegistry::init('User');

       $validUser = array('username' => 'test1', 'passwd' => null);
       $this->assertTrue($this->User->validateLogin($validUser),
                         'User was not found, but should have been:
%s');
   }
}

On Nov 26, 12:57 pm, pragna <a...@andyjordan.com> wrote:
> Mark:
> Yeah, but the fixture file I've posted was created using the
> 'fixturize' script from the guys at debuggable:http://debuggable.com/posts/fixturize-shell---generate-your-fixtures-...
>
> It reads from an existing table (in the default db) and also reads
> records in that table to build a fixture which contains $records but,
> as you've pointed out, does not include an import statement for a
> table or model. I think this is probably intentional but I haven't
> been able to get the Debuggable example working.
>
> This is what I mean about everyone seems to be building their unit
> tests slightly differently.
> Andy
>
[snip]




--~--~---------~--~----~------------~-------~--~----~
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: