【发布时间】:2011-05-08 17:21:58
【问题描述】:
我有一组测试,我想测试我的类是否在正确的时间抛出异常。在示例中,我的类使用了 __get() 魔术方法,因此我需要测试检索到无效属性时是否引发异常:
function testExceptionThrownWhenGettingInvalidProperty() {
$obj = new MyClass();
$this->setExpectedException("Property qwerty does not exist");
$qwerty = $obj->qwerty;
}
该类按应有的方式抛出一个错误,但不是仅仅获得通过,而是没有捕获异常!
There was 1 error:
1) QueryTest::testExceptionThrownWhenGettingInvalidProperty
Exception: Property qwerty does not exist
我之前使用过 SimpleTest,$this->expectException(new Exception("Property qwerty does not exist")); 工作得很好。我知道还有其他方法(@expectedException 和 try-catch),但是这个应该可以工作,而且看起来更干净。有什么想法可以让我完成这项工作吗?
【问题讨论】:
标签: php unit-testing exception phpunit