Is it possible to test an abort that was called inside a model?

Posted 7 months ago by markandrewkato

Hi everyone,

I want to unit test my User Model if the assignRole method would throw an error if an invalid role name was entered. The method inside looks like this:

public function assignRole($role)
        $role = Role::where('name', $role)->first();

        if (! $role) {
            abort(422, "Invalid role assigned");


It's working. I just don't know how to test it. Is this possible? If not, what's the best way to do this? Or am I doing something wrong here?

Any help would be appreciated. Thanks!

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.