phpunit 不会抛出异常

标签 php phpunit

php单元测试:

public function testSizeOver64K() {
        try {
            $this->login();
            $scriptname = 'test script4';
            $this->fixture->installScript($scriptname, $this->scripts[$scriptname]);
        }
        catch (Exception $expected) {
            return;
        }
        $this->fail('An expected exception has not been raised.');

    }

它调用的函数方法

function installScript($scriptname, $script, $makeactive = false)
    {
        $this->cmdPutScript($scriptname, $script);

        if ($makeactive)
            $this->cmdSetActive($scriptname);

        return true;
    }
private function cmdPutScript($scriptname, $scriptdata)
    {
        if (self::STATE_TRANSACTION != $this->state) {
            $msg = 'Not currently in TRANSACTION state';
            $code = 1;
            throw new Exception($msg, $code);
        }

        $stringLength = $this->getLineLength($scriptdata);

        $this->doCmd(sprintf("PUTSCRIPT \"%s\" {%d+}\r\n%s", $scriptname, $stringLength, $scriptdata));

        return true;
    }
private function getLineLength($string) {
        if (extension_loaded('mbstring') || @dl(PHP_SHLIB_PREFIX.'mbstring.'.PHP_SHLIB_SUFFIX)) {
            $lenght = mb_strlen($string,'8bit');
            if ( $lenght > 65536 ) {
                $msg = "Script is over 64K";
                throw new Exception($msg);
            }   
            return $lenght;
        } else {
            $lenght = strlen($string);
            if ( $lenght > 65536 ) {
                $msg = "Script is over 64K";
                throw new Exception($msg);
            }   
            return $lenght;
        }
    }

有人可以给出为什么 phpunit 没有捕获异常的提示吗?

最佳答案

使用调试器并逐步执行测试用例,以确保您的代码实际上首先抛出异常。从您的代码中无法判断环境的设置方式是否会导致异常。


顺便说一句,您应该抛出较少的通用异常。您正在使用 try/catch,因此以下内容不适用于您手头的问题,但请注意

Implemented GH-88: @expectedException (and setExpectedException()) no longer accept Exception as the expected exception class.

参见changelog for PHPUnit 3.6https://github.com/sebastianbergmann/phpunit/pull/88

关于phpunit 不会抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5157120/

相关文章:

php - 如何通过网络访问处理命令行权限

php - 将 Postgre 中一列中的数字舍入到最接近的整数

laravel - Laravel:如何在PhpUnit上启用stacktrace错误

php - 在 PHPUnit 中设置白名单

php - Laravel 使用查询字符串对路由进行单元测试

Laravel 中间件共享变量 View 在编写 HTTP 测试时不起作用

php - 使用 PHP 设置 LESS CSS 颜色

php - Laravel 表单模型绑定(bind)未填充的一对一关系

php - 如何按位比较字符串

PHPUnit 使用注解断言异常与方法调用