-
Notifications
You must be signed in to change notification settings - Fork 11.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Exceptions::fake() not working after upgrading from Laravel v8 to v11 #52010
Comments
Heya, thanks for reporting. We'll need more info and/or code to debug this further. Can you please create a repository with the command below, commit the code that reproduces the issue as one separate commit on the main/master branch and share the repository here? Please make sure that you have the latest version of the Laravel installer in order to run this command. Please also make sure you have both Git & the GitHub CLI tool properly set up. laravel new bug-report --github="--public" Please do not amend and create a separate commit with your custom changes. After you've posted the repository, we'll try to reproduce the issue. Thanks! |
I'm not sure it will reproduce on v11 from scratch, I need to create v10 or below and upgrade it first |
We’ll need to figure out what’s missing from the upgrade guide if this isn’t working for you. Please indeed provide a repo and commit the upgrade steps separately |
I don't think it works in the way you expect. If you don't call a route it does not go through the exception handling pipeline and you have to specify on the test what exception you're expecting because it's not a framework exception but a direct call to a service/class etc.. The exception you see in the console is handled by the try/catch in the PHPUnit side and the exception/error does not reach the application exception handler. |
In short, you cannot do this: public function testNotWorkingToo(): void
{
Exceptions::fake();
throw new \DomainException('The order was invalid.');
Exceptions::assertReported(function (\DomainException $e) {
return $e->getMessage() === 'The order was invalid.';
});
} and expect that the exception magically gets silenced/swallowed. |
Thanks @donnysim |
Hello guys! Here is my test code <?php
namespace Tests\Unit;
use App\Exceptions\UnknownRoleException;
use App\Models\User;
use Illuminate\Support\Facades\Exceptions;
use PHPUnit\Framework\TestCase;
class UserTest extends TestCase
{
/**
* A basic unit test example.
*/
public function test_set_roles_for_only_known_roles(): void
{
$user = new User;
Exceptions::fake();
$user->setRoles(['guest']);
Exceptions::assertReported(UnknownRoleException::class);
}
} This is the error I get when the test fails: |
Laravel Version
11.11.1
PHP Version
8.2.18
Database Driver & Version
No response
Description
I'm trying to use new feature
Exceptions::fake()
, but it just ignored. Not in HTTP tests, but in integration ones.I've double checked that
Concerns\InteractsWithExceptionHandling
trait is exists in base test case class.After upgrade I haven't migrated app to the new slim structure as it stated in upgrade guide:
Steps To Reproduce
This test is working well:
This test is failing because it's not handling exceptions:
This one is not working too:
The text was updated successfully, but these errors were encountered: