views:

763

answers:

2

I am having difficulties testing Controllers in Codeigniter: I use Toast but when I invoke my Home Controller class I get an exception that "db" is not defined. Has anybody an idea how to test this 1-1?

Thanks

class Home_tests extends Toast {


function  __construct() {
    parent::__construct(__FILE__);
// Load any models, libraries etc. you need here
}


function test_select_user() {
    $controller = new Home();
    $controller->getDbUser('[email protected]','password');
    assert($query->num_rows() == 0 );
}
 }
A: 

You might need to edit your database connectivity settings in ../system/application/config/database.php

Randell
That's not the problem, but out-of-the box its somehow not possible to call a controller from another one, and with Toast this is the case since the test base class inherits from Controller.
My db settings are ok. I can test my model classes now via model loading in the test class, but not controller classes.
In CodeIgniter, you're not supposed to declare/instantiate/call a Controller from another Controller. I think that's where the problem is. I think what you need is a library - somewhere where you can call another object/library and something you can connect to the model, instantiate inside a controller.
Randell
Thanks, yes that's right. I have read about Wick - or something (searched for calling controller within controller) in the forums but download link is broken. So I kindof stick to unittesting and manual integration / controller testing for now.
+1  A: 

As others have mentioned, CI doesn't let you call a controller from another controller. The short reason is that controllers always create response headers (even when you don't load any views or call the output class), and you aren't allowed to send two sets of HTTP headers to the browser.

While coding Toast, I tried to hack CI to allow this, but it takes some very hairy hacking of the Loader, and I came to the conclusion that you really shouldn't put any heavy logic in your controllers anyway. IMO, for proper MVC modularity, that stuff belongs in your models, libraries and helpers (which can all be unit tested with Toast).

Jens Roland