SP1966's avatar

Codeception and Laravel Exceptions

Codeception appears to choke on Laravel exceptions. The moment an exception is thrown the test stops and is marked as failed. This happens even when the test is purposely causing an exception to be thrown and testing to see it's handled correctly.

Anybody else run into this or know a work around?

0 likes
4 replies
SP1966's avatar

Has nobody else run into this problem with Codeception when running functional tests? Not even Google has an answer, it's making me feel pretty special if you know what I mean!

mhanson01's avatar
Level 1

I don't have my work code in front of me at the moment which I've done this before, but try this out, put this line in your test right before you expect to see the exception, just make sure to reference the proper exception class:

\PHPUnit_Framework_TestCase::setExpectedException('Laracasts\Validation\FormValidationException');

edit: for reference, this works with codeception since it leverages phpunit

2 likes
SP1966's avatar

@mhanson01

Thank you, this works great so long as I'm using the Cept format but fails when using the Cest format. Any idea how to get it working in a Cest?

syahzul's avatar

You can try solution from this answer http://stackoverflow.com/a/25225086 except, I cannot use

$I->assertTrue(
   // other codes
);

Here's what I'm using right now

$i->seeExceptionThrown('Illuminate\Database\Eloquent\ModelNotFoundException', function() use ($i){
    //All actions that you expect to generate the Exception
    $i->amOnRoute('users.edit', ['id' => 10000]);
    $i->canSee('Invalid item ID!');
});
1 like

Please or to participate in this conversation.