Skip to content
Advertisement

Set Expected Exception in Codeception Functional Cept

I want to do something like:

$I->setExpectedException('LaracastsValidationFormValidationException');

In a functional cept. Any chance to do so?

PHPUnit_Framework_TestCase::setExpectedException('LaracastsValidationFormValidationException');

Above code will work in isolation but if I run codecept run, the tests get stuck once the test with the expected exception is complete.

Here’s my setup:

YML:

class_name: FunctionalTester
modules:
    enabled: [Filesystem, Db, FunctionalHelper, Laravel4, Asserts]

Advertisement

Answer

I think this is a known problem with the Laravel 4 module for codeception, not sure if it is going to be fixed soon, but in the meantime I created a helper function to test Exceptions:

In the file tests/_support/FunctionalHelper.php add the following method:

public function seeExceptionThrown($exception, $function)
{
    try
    {
        $function();
        return false;
    } catch (Exception $e) {
        if( get_class($e) == $exception ){
            return true;
        }
        return false;
    }
}

You use it in your Cepts like this:

$I = new FunctionalTester($scenario);
$I->assertTrue(
    $I->seeExceptionThrown('LaracastsValidationFormValidationException', function() use ($I){
    //All actions that you expect to generate the Exception
    $I->amOnPage('/users/edit/1');
    $I->fillField('name', '');
    $I->click('Update');
}));
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement