php - Proper way to unit test beforeFilter() and beforeRender() in AppController -


i'm writing unit tests new site i'm working on uses cakephp 2.3. everything's going smoothly, except 1 thing. want test beforefilter() , beforerender() functions. beforefilter() can test enough creating mock object

$app_controller_mock = $this->generate(     'app',     array(         'components' => array(             'session'         )     ) ); $app_controller_mock->beforefilter(); 

that run function, won't give me useful stuff $this->vars, $this->content, etc. on particular function that's ok, in beforerender() i'm manually setting viewvar want check in test case.

$app_controller_mock->beforerender(); $this->assertnotnull($this->vars['controller_name'], "'controller_name' viewvar null"); 

when above, following error:

'controller_name' viewvar null failed asserting null not null. 

what's "proper" way this?

something dispatch full request, ensures integrated components exist beforefilter , beforerender.

sometimes, dummy controller helpful:

app::uses('controllertestcase', 'testsuite'); app::uses('appcontroller', 'controller'); class mockfilterscontroller extends appcontroller {     public $controllervar = 'something';      public function action($var1 = null) {         // can helpful set other vars, or         // set on controller         $this->controllervar = $var1;         $this->set(compact($var1));     } }  class appcontrollertest extends controllertestcase {      public function testbeforerender() {         $this->testaction('/mock_filters/action/test', array(             'method' => 'get'         ));         // ensure view vars set         $this->assertequals('test', $this->vars['var1']);         // other assertions beforerender items     }  } 

then repeat beforefilter, mocking using generate() necessary.

the $this->vars, content, view, etc. variables set when using controllertestcase::testaction run integration test.

using methods seems effective me, can check variables changed on controller itself, integration various components, , of course view variables being set.


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -