1

I'm trying to intercept a UnauthorizedException with Codeception in my functional test. Well, In fact my functional test are Laravel tests triggered by codeception. So, I tried 2 ways:

/** @test
 *
 * @expectedException UnauthorizedException
 */
public function test_exception()
{

    $this->visit("/associations/1/edit")

}

Or

 /** @test
 *
 */
public function test_exception()
{
   \PHPUnit_Framework_TestCase::setExpectedException(UnauthorizedException::class);
    $this->visit("/associations/1/edit")

}

In my controller, I just trow it to test:

 throw new UnauthorizedException();

In both cases, it just says:

Failed asserting that exception of type "UnauthorizedException" is thrown.

Everything seems fine, but it doesn't seem to work... Any idea why???

EDIT : My Edit Method in Controller

public function edit($id)
{

    $association = Association::findOrFail($id);
    $federation = $association->federation;

    if (Auth::user()->cannot('edit', $association)) { // I have checked with dd() my test goes here.
        throw new UnauthorizedException();
    }

    $users = User::all();
    $federations = Federation::all();

    return view('associations.form', compact('association', 'users', 'federations', 'federation'));
}
18
  • tried namespacing? \Illuminate\Contracts\Validation\UnauthorizedException Commented Jun 17, 2016 at 23:01
  • I use : use Illuminate\Contracts\Validation\UnauthorizedException; shoudn't it be the same thing? Commented Jun 17, 2016 at 23:02
  • Yes it should. Can you post the full controller method Commented Jun 17, 2016 at 23:04
  • which one???? edit or update? Commented Jun 17, 2016 at 23:05
  • The one that throws the exception Commented Jun 17, 2016 at 23:06

1 Answer 1

0

I finally got the solution from here:

Basic idea, I guess, is exception is catched and sent to Handler where a view is returned.

So, for testing, you should include in Handler.php the condition:

public function render($request, Exception $e)
{
    if (App::environment() == 'testing') {
        throw $e;
    }
    // Your exception management...

and now Exception is thrown!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.