Jeff
Jeff

Reputation:

How to test controllers with CodeIgniter PART 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 );
}
 }

Upvotes: 1

Views: 2095

Answers (2)

Jens Roland
Jens Roland

Reputation: 27770

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).

Upvotes: 8

Randell
Randell

Reputation: 6170

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

Upvotes: 0

Related Questions