我正在用phpUnit編寫一些單元測試來測試一個Zend Framework應用程序,並且我在changePassword函數中測試了一個異常時遇到了一些問題。測試不會失敗,但是在生成html的覆蓋工具中,「拋出新的異常($ tr-> translate('userOldPasswordIncorrect'));」線沒有經過測試。未使用phpUnit進行測試的異常?
public function changePassword(array $data, $id)
{
$user = $this->_em->find('Entities\User', (int) $id);
$oldPassword = sha1(self::$_salt . $data['oldPassword']);
if ($user->getPassword() !== $oldPassword) {
$tr = PC_Translate_MySQL::getInstance();
throw new Exception($tr->translate('userOldPasswordIncorrect'));
}
$user->setPassword(sha1(self::$_salt . $data['password']));
$this->_em->persist($user);
$this->_em->flush();
}
這應該測試異常的單元測試:
/**
* @depends testFindByAuth
* @expectedException Exception
*/
public function testChangePasswordWrongOldPassword()
{
$this->_dummyUser = $this->_user->findByAuth($this->_dummyEmail, $this->_dummyPassword, $this->_reseller);
// Try to change the password with a wrong oldPassword
$data['oldPassword'] = 'wrongOldPassword';
$data['password'] = $this->_dummyNewPassword;
$this->_user->changePassword($data, $this->_dummyUser->getId());
}
我希望有人能告訴我什麼,我做錯了。
更新
問題是PC_Translate_MySQL :: getInstance()方法內。有人拋出異常。正如我正在測試的那樣,通過了一個普遍的例外。解決方案不要在changePassword方法中使用常規異常。
是testFindByAuth傳遞?如果沒有,取決於不會讓testChangePasswordWrongOldPassword運行。 – 2010-08-12 17:40:26
testFindByAuth正在通過。 – tom 2010-08-12 17:44:22