牛骨文教育服务平台(让学习变的简单)

@expectedExceptionCode

@expectedExceptionCode 标注与 @expectedException 联合使用,可以对抛出异常的代码作出断言,这样可以缩小具体异常的范围。

class MyTest extends PHPUnit_Framework_TestCase
{
    /**
     * @expectedException     MyException
     * @expectedExceptionCode 20
     */
    public function testExceptionHasErrorcode20()
    {
        throw new MyException("Some Message", 20);
    }
}

为了方便测试并减少冗余,可以用"@expectedExceptionCode ClassName::CONST"这样的语法将指定类常量作为 @expectedExceptionCode

class MyTest extends PHPUnit_Framework_TestCase
{
    /**
      * @expectedException     MyException
      * @expectedExceptionCode MyClass::ERRORCODE
      */
    public function testExceptionHasErrorcode20()
    {
      throw new MyException("Some Message", 20);
    }
}
class MyClass
{
    const ERRORCODE = 20;
}