Summer Sale! All accounts are 50% off this week.

PetroGromovo's avatar

How with expectException to get exception message ?

Making tests on laravel 9 site I try to catch custom Exception like

    $this->expectException(\App\Exceptions\UserAccountManagerAccessException::class);

    $response = $this->actingAs($loggedUser, 'api')->post(route('articles.store'),

But as UserAccountManagerAccessException is raised in different places with 1 string parameter(message) if there is a way to catch and check this message text ?

"laravel/framework": "^9.19",

Thanks in advance!

0 likes
1 reply
tisuchi's avatar
tisuchi
Best Answer
Level 70

@petrogromovo Try this:

$this->expectException(\App\Exceptions\UserAccountManagerAccessException::class);
$this->expectExceptionMessage('The user does not have access to perform this action');

$response = $this->actingAs($loggedUser, 'api')->post(route('articles.store'), ...
2 likes

Please or to participate in this conversation.