Nice Mark! Every time people write about testing in Cake I get excited… until I try to do it myself. I just have to layout the starting point I guess.

primeminister on 12/18/08

Thanks for this Mark.
Just starting to work my way through it.

In startTest(), should $this->Posts->Component->initialize();
$this->Posts->Component->initialize($this->Posts); ?


pragnatek on 12/18/08

pragnatek: You are correct, fixed it. Guess that’s what happens when you start writing at 11pm.

mark story on 12/18/08

Hi.. i’m newbie. How to run all of test things above? using CLI ? can u tell me how to run those tests?

Thx :)

polutan on 12/23/08

polutan: You need to setup simpleTest. Once simpletest is setup you can run unit tests from either the CLI or a web browser. Perhaps reading the section of the book related to getting unit testing setup would help there.

mark story on 12/24/08

Hey mark :) thank you very much. I will try..

polutan on 12/24/08

I am following the same pattern for my post controller with fixture. my test runs on live database instead of using $test. Here is my fixture :
class PostFixture extends CakeTestFixture { var $name = ‘Post’; var $import = array(‘table’ => ‘posts’, ‘records’ => false);
var $records = array( array (‘id’ => 1,‘user_id’ => 1, ‘name’ => ‘First Article’, ‘body’ => ‘First Article Body’, ‘status’ => ‘1’, ‘created’ => ’2007-03-18 10:39:23’, ‘modified’ => ’2007-03-18 10:41:31’), array (‘id’ => 2,‘user_id’ => 1, ‘name’ => ‘Second Article’, ‘body’ => ‘Second Article Body’, ‘status’ => ‘1’, ‘created’ => ’2007-03-18 10:41:23’, ‘modified’ => ’2007-03-18 10:43:31’), array (‘id’ => 3, ‘user_id’ => 1,‘name’ => ‘Third Article’, ‘body’ => ‘Third Article Body’, ‘status’ => ‘1’, ‘created’ => ’2007-03-18 10:43:23’, ‘modified’ => ’2007-03-18 10:45:31’) );

this is config/database.php

var $default = array( ‘driver’ => ‘mysql’, ‘persistent’ => false, ‘host’ => ‘localhost’, ‘login’ => ‘root’, ‘password’ => ‘’, ‘database’ => ‘cake12’, ‘prefix’ => ‘’, );

var $test = array( ‘driver’ => ‘mysql’, ‘persistent’ => false, ‘host’ => ‘localhost’, ‘login’ => ‘root’, ‘password’ => ‘’, ‘database’ => ‘test_cake12’, ‘prefix’ => ‘tes_suite_’, );

chirayu on 1/1/09

Does this technique still work with the fixtures as described in Thanks for writing an illuminating article!

Rich Yumul on 1/12/09

Eh, please disregard my previous post. I just reviewed the article in a little more detail…

Rich Yumul on 1/12/09

Thanks a lot for this article.

I stopped testing part of my controllers functionality because I wanted to use fixtures and sessions, but now you gave me the key :-).

Javier on 1/14/09

Thanks for sharing this. I am excited to try this after several failed attempts to test controllers.

If I implement Cake’s ACL, does that mean I need to create fixtures for the ACL tables (aros, acos, and aros_acos)?

Hans on 1/27/09

Hi Mark thanks for this tutorial. This saved my day..

Dinesh on 4/29/09

Great Write Up! I’ve been scouring the net to get some more insight on how to test my CakeApps, and this was the ticket!

Robert Navarro on 5/2/09

Hi Mark. I’m heavily using this approach in my tests but I found that you could need to App::import(‘Model’) frist to get the controller instantiate the right class, at least in plugins.

I found that my plugins controllers tests failed beacause controller instantiated the model class as generic AppModel class.

Adding App::import(‘Plugin.Model’) did the trick.

Fran Iglesias on 7/5/09

Fran: An alternative is to set $this->plugin on the controller. This will let the controller know it is inside a plugin and load the correct model. See Controller::loadModel() for how this works.

mark story on 7/7/09

Thank you again Mark. I’ve just discovered it by myself after reading the controller.php core code.

But now, I’m aware that one must set all relevant params of the controller in order to test.

Fran Iglesias on 7/7/09

Thanks so much for this article. I’ve recently gotten excited about cake testing.

Just a thought, I know its probably outside protocol, but maybe you could post a link to this site from the official cakephp testing page?

under pitfalls it links to the ticket, but it might be more useful to link here?

Nico on 11/6/09

Thanks so much for this, I got it working (almost) except 1 tiny problem:

I was getting this error:

Unexpected PHP error [Undefined index: action] severity [E_NOTICE] in [\cake\cake\libs\controller\components\auth.php line 266]

On that very specific line of the auth.php we have:
$action = strtolower($controller->params[‘action’]);

I think the problem is obvious, ‘action’ was not set. But I wonder how should I go about fixing this? I thought this should be set automatically by cake?

raine on 12/27/09

Had the same problem as raine. I think it’s because we’re not using the Router so the params don’t get set for the url being ‘called’. At this point it gets complicated for those that have multiple admin sections with different prefixes and tests for them in the beforeFilter() of the controller.

Is there any way of calling urls using the test controller? maybe ‘/test_posts/index’ with testAction? (it doesn’t work with your setup since it’s not in the controllers folder… but putting it there would be a nice mess, mixing testing and real code…)

dave on 2/5/10

to fix this error in your test(see raine comment):
Unexpected PHP error [Undefined index: action] severity [E_NOTICE] in [\cake\cake\libs\controller\components\auth.php line 266]

you could add :
in function startCase(){….


Francisco Quiñones on 2/13/10