I have a test suite, and I want to check that my classes throw exceptions at the right time. In this example, my class uses the __get () magic method, so I need to check if an exception occurs when I get an invalid property:
function testExceptionThrownWhenGettingInvalidProperty() { $obj = new MyClass(); $this->setExpectedException("Property qwerty does not exist"); $qwerty = $obj->qwerty; }
The class throws an error as it should, but instead of just getting a pass, the exception is not caught!
There was 1 error: 1) QueryTest::testExceptionThrownWhenGettingInvalidProperty Exception: Property qwerty does not exist
I used SimpleTest before, and $this->expectException(new Exception("Property qwerty does not exist")); worked fine. I know there are other methods (@expectedException and try-catch), but this one should work and it looks a lot cleaner. Any ideas how I can make this work?
php exception unit-testing phpunit
Nathan macinnes
source share