我正在爲我的一個控制器編寫CakePHP單元測試。控制器有幾個調用AuthComponent::user()
方法,讀取當前登錄用戶的數據。有3個用途:在CakePHP中嘲諷authed用戶
AuthComponent::user()
(無參數,可以獲取整個陣列)AuthComponent::user('id')
(提取的用戶ID)AuthComponent::user('name')
(獲取的用戶名)
我已經嘗試了兩種在我的測試中嘲諷AuthComponent的方法:
// Mock the Controller and the Components
$this->controller = $this->generate('Accounts', array(
'components' => array(
'Session', 'Auth' => array('user'), 'Acl'
)
));
// Method 1, write the entire user array
$this->controller->Auth->staticExpects($this->any())->method('user')
->will($this->returnValue(array(
'id' => 2,
'username' => 'admin',
'group_id' => 1
)));
// Method 2, specifically mock the AuthComponent::user('id') method
$this->controller->Auth->staticExpects($this->any())->method('user')
->with('id')
->will($this->returnValue(2));
Thes但是,方法不適用於我。方法1似乎沒有做任何事情,我的控制器中使用當前登錄用戶的id的保存操作返回null,所以這個值沒有被正確設置/獲得。
方法2似乎工作,但過於寬泛,它也試圖將自身綁定到AuthComponent::user()
調用(在一個沒有PARAMS),它失敗,出現錯誤:
Expectation failed for method name is equal to when invoked zero or more times Parameter 0 for invocation AuthComponent::user(null) does not match expected value. Failed asserting that null matches expected 'id'.
我怎樣才能得到正確的嘲笑AuthComponent所以可以獲得所有的字段/變量?
謝謝,這讓我度過了煩人的錯誤,並且是一個很好的構造,可以快速模擬Auth對所有測試的感受。在開始工作之前(由於某種原因靜態方法一直返回null),我最終在控制器中用'$ this-> Auth-> user'調用了靜態'AuthComponent :: user'調用。 – Oldskool