SimpleTest:如何断言抛出PHP错误?

And*_*rew 7 php unit-testing simpletest

如果我是正确的,SimpleTest将允许您断言抛出PHP错误.但是,根据文档,我无法弄清楚如何使用它.我想声明我传递给构造函数的对象是一个实例MyOtherObject

class Object {
    public function __construct(MyOtherObject $object) {
        //do something with $object
    }
}

//...and in my test I have...
public function testConstruct_ExpectsAnInstanceOfMyOtherObject() {
    $notAnObject = 'foobar';
    $object = new Object($notAnObject);
    $this->expectError($object);
}
Run Code Online (Sandbox Code Playgroud)

我哪里错了?

Law*_*nti 13

类型提示抛出E_RECOVERABLE_ERROR,自PHP版本5.2以来可以被SimpleTest捕获.以下将捕获包含文本"必须是实例"的任何错误.PatternExpectation的构造函数采用perl正则表达式.

public function testConstruct_ExpectsAnInstanceOfMyOtherObject() {
    $notAnObject = 'foobar';
    $this->expectError(new PatternExpectation("/must be an instance of/i"));
    $object = new Object($notAnObject);
}
Run Code Online (Sandbox Code Playgroud)