public function AddClassTest::testAddWithBadDataProvider in Examples for Developers 8
Same name and namespace in other branches
- 3.x modules/phpunit_example/tests/src/Unit/AddClassTest.php \Drupal\Tests\phpunit_example\Unit\AddClassTest::testAddWithBadDataProvider()
Test AddClass::add() with data that should throw an exception.
This method is similar to testAddWithDataProvider(), but the data provider gives us data that should throw an exception.
This test uses the setExpectedException() method to tell PHPUnit that a thrown exception should pass the test. You specify a fully-qualified exception class name. If you specify \Exception, PHPUnit will pass any exception, whereas a more specific subclass of \Exception will require that exception type to be thrown.
Alternately, you can use try and catch blocks with assertions in order to test exceptions. We won't demonstrate that here; it's a much better idea to test your exceptions with setExpectedException().
@dataProvider addBadDataProvider
See also
File
- phpunit_example/
tests/ src/ Unit/ AddClassTest.php, line 112
Class
- AddClassTest
- AddClass units tests.
Namespace
Drupal\Tests\phpunit_example\UnitCode
public function testAddWithBadDataProvider($a, $b) {
$sut = new AddClass();
$this
->expectException(\InvalidArgumentException::class);
$sut
->add($a, $b);
}