2011-11-18 62 views
0

我試圖測試CakePHP操作,它處理用Security組件保護的註冊表單。我已經在UsersController配置的部件是這樣的:處理CakePHP 2測試用例中的安全組件

public $components = array(
    'Security' => array('unlockedFields' => array('password_again')), 
); 

我可以在瀏覽器上執行的動作,但每當運行測試情況下,它失敗,出現以下消息:

"The request has been black-holed" 

我有試圖禁止多種方式測試盒內的驗證:

$this->Users->Security->enable = false; 
$this->Users->Security->validatePost = false; 
$this->Users->Components->disable('Security'); 
$this->Users->Security = null; // desperate measure :) 

// still fails 
$this->testAction('/signup', array('data' => array(...), 'return' => 'contents')); 

但在使用的Security驗證測試堅持0請求。我正在使用CakePHP 2.0.3和PHPUnit 3.6.3。順便說一句,我不直接使用UsersController,而是使用CakePHP爲我烘焙的TestUsersController類(作爲generate方法的替代品,我認爲)。

在測試用例中處理安全組件的正確方法是什麼?

回答

3

的解決方案是嘲笑Users控制器和User模型併爲Security::_validatePost()方法期望:

$this->Users = $this->generate(
    'Users', 
    array(
    'components' => array(
     'Security' => array('_validatePost'), 
    ), 
    'models' => array('User' => true), 
) 
); 
$this->Users->Security->expects($this->any()) 
    ->method('_validatePost') 
    ->will($this->returnValue(true));