Using mocks to test controllers
In this recipe we will learn how to extend what we have covered in the previous recipe by using mocks, an indispensable tool for building powerful test cases.
Getting ready
To go through this recipe, we need unit tests already in place. Go through the previous recipe.
How to do it...
1. Edit your
app/tests/cases/controllers/articles_controller.test.php
file and place the following code at the beginning, right before the declaration of the classArticlesControllerTestCase
:App::import('Controller', 'Articles'); class TestArticlesController extends ArticlesController { public $name = 'Articles'; public $testRedirect = false; public function __construct() { parent::__construct(); Configure::write('controllers.'.$this->name, $this); } public function beforeFilter() { if (isset($this->Session)) { App::import('Component', 'Session'); Mock::generate('SessionComponent'); $this->Session = new MockSessionComponent(); } parent::beforeFilter(); } public function...