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'));
}
\Illuminate\Contracts\Validation\UnauthorizedException