Search code examples
exceptionphpspec

How to catch exceptions with phpspec2


I'm trying to catch exceptions with my spec but i can't make it works. This is my code:

$this->edit('updated comment', $comment, $user)
    ->shouldReturnAnInstanceOf('\Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException');

What i'm doing wrong?


Solution

  • According to documentation, you can do something like this using Matcher for testing exceptions:

    $this
       ->shouldThrow('Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException')
       ->during('edit', array('updated comment', $comment, $user));
    

    You can also take a look into cookbook which is in phpspec repository on Github.