UnionOfRAD/lithium — Issue #280: lithium\test\Unit->expectException not working as expected
Author: clexmond
Created: 2012-01-17 17:00:41+00:00
State: open

In most (maybe all?) testing frameworks, expecting an exception that doesn't fire should result in a test failure. Lithium's current implementation correctly avoids dumping a stack trace and breaking on an expected exception, but it also fails silently if the expected exception is not fired.

Furthermore, expectException is being used incorrectly in several places throughout the core lithium test cases. One such example is in lithium\tests\cases\action\ControllerTest->testMethodInvocation where the line throwing the exception is followed by several more asserts which will never run (snippet posted, there are actually more):

```php
$postsController = new MockPostsController();
$this->expectException('/Unhandled media type/');
$result = $postsController(null, array('action' => 'index', 'args' => array(true)));

$this->assertTrue(is_a($result, 'lithium\action\Response'));
$this->assertEqual($result->body, '');

$headers = array('Content-type' => 'text/html; charset=UTF-8');
$this->assertEqual($result->headers, $headers);
```

If the test cases are written as desired, adding a lithium\test\Unit->fail method and checking for exceptions in try catch blocks would be a good replacement. Otherwise, expectException needs to be treated as its own assert and explicitly fail if the expected exception isn't thrown. Furthermore, the tests would need to be rewritten to make sure no asserts occur *after* expectExceptions as they will clearly never run.