codeception/assert-throws

Assert exception was thrown without stopping a test

1.0.2 2019-08-15 13:29 UTC

README

Build Status

Handle exceptions inside a test without a stop!

Assertions for exceptions. Works with PHPUnit and Codeception.

Usage

Catch exception thrown inside a code block.

<?php
$this->assertThrows(NotFoundException::class, function() {
	$this->userController->show(999);
});

// alternatively
$this->assertThrows(new NotFoundException, function() {
	$this->userController->show(999);
});
?>

You can optionally test the exception message:

<?php
$this->assertThrowsWithMessage(NotFoundException::class, 'my error message', function() {
	throw new NotFoundException('my error message');
});
?>

Installation

composer require codeception/assert-throws --dev

Include AssertThrows trait it to a TestCase:

<?php
class MyTest extends PHPUnit\Framework\TestCase
{
    use Codeception\AssertThrows;

} 

License MIT