dongsheng8664 2010-08-12 17:37
浏览 109
已采纳

没有用phpUnit测试异常?

I'm writing some unit test with phpUnit to test a Zend Framework application and I've got some issues with testing an exception in the changePassword function. The test doesn't fail, but in the coverage tool which generates html the "throw new Exception($tr->translate('userOldPasswordIncorrect'));" line isn't tested.

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();
}

The unit test which should test the exception:

/**
 * @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());
}

I'll hope somebody can tell me what I'm doing wrong.

Update

The problem was inside the PC_Translate_MySQL::getInstance() method. There was thrown an exception. And as I was testing on getting a general exception this ofcourse passed. Solution don't use a general Exception in the changePassword method.

  • 写回答

1条回答 默认 最新

  • dqusbxh44823 2010-08-12 18:00
    关注

    My guess? There's an exception being thrown from PC_Translate_MySQL::getInstance()...

    That's the trouble with using a single Exception. It makes it tougher to check what exception was thrown. I'd suggest changing the changePassword method to throw a different exception. Perhaps a InvalidArgumentException, or a RuntimeException. And then test for that one.

    Personally, I use custom exceptions all the time for this reason.

    try {
    } catch (DatabaseQueryException $e) {
        // Handle database error
    } catch (DatabaseConnectionException $e) {
        // We never even connected...
    } catch (InvalidArgumentException $e) {
        //...
    }
    

    I, as a rule, never use catch (Exception $e) for that very reason. You never know what exception you caught. (I do have a custom exception handler defined, so I don't fatal if the application doesn't catch. Instead it shows a 500 error and logs the exception)...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀