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

Posted 4 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");
        }

        $this->roles()->attach($role->id);
    }

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.

Laracasts Mascot

Hi, Have We Met Yet?

Did you know that, in addition to the forum, Laracasts includes well over 1000 lessons on modern web development? All for the price of one lunch out per month.

Sign Me Up

Channels

Reply to

Use Markdown with GitHub-flavored code blocks.