4 ms·
In case anyone is wondering, that code should read something like: void testFooBarException() { try { thisShouldThrowFooBarException() fail()
by ilyanep 13y ago
In case anyone is wondering, that code should read something like:
void testFooBarException() {
try {
thisShouldThrowFooBarException()
fail() // Should have thrown exception
} catch (FooBarException ignored) {}
}
If you're feeling super fancy, you can even do some asserts in the catch block to make sure the FooBarException has an expected exception message.
- deathanatos 13y agoI'm glad I work in a language where this is assertRaises(FooBarException, thisShouldThrowFooBarException) Impossible to mistype, and states exactly what should happen.
- TeMPOraL 13y agoFirst thing to do when working with the former is to write some code that will allow you to write the latter.
- rdeboo 13y agoWith Junit, just write @Test(expected = FooBarException.class) void testFooBarException() { thisShouldThrowFooBarException() }
- jaredmcateer 13y agoPHPUnit has this in the comment annotations /** * Tests that thisShouldThrowFooBarException throws FooBarException * * @expectedException FooBarException */ public function testFooBarException() { thisShouldThrowFooBarException(); } but I much prefer /** * Tests that thisShouldThrowFooBarException throws FooBarException */ public function testFooBarException() { $this->setExpectedException('FooBarException'); thisShouldThrowFooBarException(); } Much more explicit and you're less likely to miss it when trying to grok someone else's unittest.