我正在用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