You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Apr 12, 2018. It is now read-only.
You should be as specific as possible when testing exceptions. Testing for classes that are too generic might lead to undesirable side-effects. Accordingly, testing for the Exception class with @ExpectedException or setExpectedException() is no longer permitted.
I tested this and the tests pass:
<?phpnamespaceUnitTests\ExceptionsTest;
class MyClass
{
protected$aString;
publicfunction__construct($aString)
{
if (empty($aString)) {
thrownew \Exception('Empty string provided.');
}
$this->aString = $aString;
}
publicfunctioncheckString()
{
if (is_string($this->aString)) {
returntrue;
} else {
thrownew \Exception('This is not a string!');
}
}
}
<?phpuseUnitTests\ExceptionsTest\MyClass;
class MyClassTest extends \PHPUnit_Framework_TestCase
{
/** * instantiating the constructor with an empty string throws an exception * @expectedException \Exception */publicfunctiontestInstantiatingTheConstructorWithAnEmptyStringThrowsAnException()
{
$myClass = newMyClass('');
}
/** * instantiating the constructor with a number and calling checkString will throw an exception * @expectedException \Exception */publicfunctiontestInstantiatingTheConstructorWithANumberAndCallingCheckStringWillThrowAnException()
{
$myClass = newMyClass(11);
$myClass->checkString();
}
}
Aparently testing for Exception is permitted. I'm using:
$ php phpunit.phar -c tests/phpunit.xml
PHPUnit 4.6.4 by Sebastian Bergmann and contributors.
The text was updated successfully, but these errors were encountered:
Simply replace 6.5 with the version number you are looking for. Simply replace en with fr, pt_br, ja, or zh_cn to access the French, Brazilian Portuguese, Japanese, or Simplified Chinese translation, respectively.
Starting with the documentation for PHPUnit 7.0, the PHPUnit documentation is hosted at https://phpunit.readthedocs.io/.
I am sorry that I have to close this issue here as GitHub does not support moving issues from one project to another. Please open a new ticket for this issue in one of the new issue trackers (see above).
https://phpunit.de/manual/current/en/writing-tests-for-phpunit.html#writing-tests-for-phpunit.exceptions.examples.ErrorTest.php
There is a not on the link above saying:
I tested this and the tests pass:
Aparently testing for
Exception
is permitted. I'm using:The text was updated successfully, but these errors were encountered: