Skip to content

Instantly share code, notes, and snippets.

@cmbuckley
Created November 3, 2012 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cmbuckley/4007523 to your computer and use it in GitHub Desktop.
Save cmbuckley/4007523 to your computer and use it in GitHub Desktop.
Erroneous exceptions in PHPUnit for http://stackoverflow.com/q/13198392
<?php
include 'fixture.php';
class AnnotationTest extends PHPUnit_Framework_TestCase {
/**
* @expectedException MyException
*/
public function testExceptionThrown() {
$fixture = new Fixture();
$fixture->methodThatThrows();
}
}
<?php
class Fixture {
public function __construct() {
// an exception is erroneously being thrown here
throw new MyException('This is a bug that will be masked by the annotation');
}
public function methodThatThrows() {
throw new MyException('This is the expected exception');
}
}
class MyException extends Exception {}
<?php
include 'fixture.php';
class MethodTest extends PHPUnit_Framework_TestCase {
public function testExceptionThrown() {
$fixture = new Fixture();
// with the method approach, I can control exactly when the Exception is expected
$this->setExpectedException('MyException');
$fixture->methodThatThrows();
}
}
$ phpunit annotation.php
PHPUnit 3.6.10 by Sebastian Bergmann.
.
OK (1 test, 1 assertion)
$ phpunit method.php
PHPUnit 3.6.10 by Sebastian Bergmann.
E
There was 1 error:
1) AnnotationTest::testExceptionThrown
MyException: This is a bug that will be masked by the annotation
fixture.php:6
method.php:8
FAILURES!
Tests: 1, Assertions: 0, Errors: 1.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment