我正在爲使用CakePHP 2.3的新站點編寫單元測試。一切都很順利,除了一件事。我想測試beforeFilter()和beforeRender()函數。 beforeFilter(),我可以通過創建一個模擬對象在AppController中對beforeFilter()和beforeRender()進行單元測試的正確方法
$app_controller_mock = $this->generate(
'App',
array(
'components' => array(
'Session'
)
)
);
$app_controller_mock->beforeFilter();
將運行功能測試很輕鬆了,但它不會給我像$this->vars
的有用的東西,$this->content
,等等。在這個特殊的功能,那沒關係,但在我的beforeRender()我手動設置我想在我的測試用例中檢查的viewVar。
$app_controller_mock->beforeRender();
$this->assertNotNull($this->vars['controller_name'], "'controller_name' viewVar is null");
當我做以上,我得到以下錯誤:
'controller_name' viewVar is null
Failed asserting that null is not null.
什麼是「正確」的方式做到這一點?
不確定,但不應該viewVars位於'$ app_controller-> viewVars ['xxx']'? – thaJeztah 2013-05-10 22:46:46
他們是。我想使用內置的testAction()。我想手動調用它,只是檢查viewVars將暫時工作。如果我的應用程序變得更復雜,但它可能會變得毛茸茸。 – Derek 2013-05-14 21:49:23
@Derek我發佈了一個答案,剛纔注意到這個問題有多大,但我希望它能幫助未來的某個人(因爲我相信你現在已經找到了適合你的解決方案) – jeremyharris 2015-04-17 19:40:39