How to fix Laravel Auth Mocking not working?

  laravel, mockery, phpunit

I’m trying to mock the Auth facade so I can mock the Auth::user() method. I need this because I source my users from another database.

In production, this all works fine. However, when I try and write tests for this and try something like;

$user = User::factory()->create();

$response = $this->postJson(route('api.login'), [
    'username' => 'username',
    'password' => 'test-password'

I get a very bizarre error that to be honest I’m not even sure what it means.

MockeryExceptionBadMethodCallException: Received Mockery_2_Illuminate_Auth_AuthManager::userResolver(), but no expectations were specified in Mockery/Loader/EvalLoader.php(34) : eval()'d code:927

I have found very little about this error online and can’t really figure out what it even means. Hopefully someone can help.

Source: Laravel

Leave a Reply