-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
PHPunit not expect generic exception? #454
Comments
Fixed in the documentation. |
For the record, this is a fairly critical issue as we are unable to throw generic exceptions, and personally I don't feel like creating custom exceptions just to pass unit testing. Is there any way you could remove this restriction? |
The restriction has been removed for PHPUnit 3.7. |
Thanks for responding. When will 3.7 be available? Stable? |
Any way this can get brought into 3.6? |
I came up with a work around until 3.7 is stable. http://melikedev.com/2012/08/24/phpunit-you-must-not-expect-the-generic-exception-class/ |
@digitalprecision that's good but less ideal - cc @sebastianbergmann - do you advise upgrade to 3.7? |
Feel free to test PHPUnit 3.7.0RC1; that is what release candidates are for. |
Inspired by @digitalprecision I designed a robust solution that is easier to bolt onto your test cases: https://gist.github.com/4298350 The gist includes 3 ways to incorporate generic exception handling into your test cases:
The gist code includes all three versions, but you can delete the versions you don't want (or can't compile) from your copy of the code. There is also a full test suite available to test the functionality. |
Much simpler to patch phpunit, something like so:
Is there a commit from 3.7 that could possibly apply to 3.6? |
@joaoinacio: Patching wasn't an option for us as we have multiple UT servers and we didn't want to be responsible for maintaining vendor code. The author was asked if he would backport to 3.6 but never responded directly to the question, rather he suggested we beta test 3.7. You can use mine or @iNamik suggested work arounds. |
For anyone using travis with 5.2 due to dependencies to other systems that refuse to drop 5.2 you can use this pattern to bypass the phpunit limitation, it's also slightly better. try {
$proc->errors(); // if this doesn't throw an exception, the next line will fail the test
throw new Exception('Failed asserting blah blah blah exception.');
}
catch (Exception $e) {
// ensure we got the right exception
$this->assertEquals('Place here the expected Exception message.', $e->getMessage());
} |
@srcspider sorry, where would I use that? In the test itself? and if so, what is the |
PHPUnit 3.6 didn’t like you to use `@expectedException` with a generic `Exception`: > InvalidArgumentException: You must not expect the generic exception class. See sebastianbergmann/phpunit#454 (comment) 71137
InvalidArgumentException: You must not expect the generic exception class,
when I test
class MyTest extends PHPUnit_Framework_TestCase
{
/**
* @ExpectedException Exception
* @expectedExceptionMessage Some Message
*/
public function testExceptionHasRightMessage()
{
throw new Exception('Some Message', 20);
}
}
with PHPUnit3.6.7
why ?
IF PHPunit not expect generic exception , why the document has this example?
The text was updated successfully, but these errors were encountered: