lundi 22 juin 2020

Mocking laravel cache helper

I want mock laravel using PESTPHP Testing Framework on cache helper but when i try i got error like this

Method Mockery_2_Illuminate_Cache_CacheManager::remember() does not exist on this mock object

I want mock rememberForever method but why the remember method get mocked. I also try to change shouldReceive to another method but always remember method to get mocked.

Here my code :

Controller :

public function index()
{
    $groups = cache()->rememberForever('groups', fn() => Group::pluck('id', 'slug'));

    return view('index', compact(
        'groups'
    ));
}

ControllerTest:

it('should return given data on index page', function () {
    /** @var \Tests\TestCase $this */

    $groups = Group::pluck('id', 'slug');

    Cache::shouldReceive('rememberForever')->once()
        ->with('groups', \Closure::class)
        ->andReturn($groups);

    actingAs()->get(route('index'))
        ->assertViewHasAll([
            'groups' => $groups,
        ]);
});

Aucun commentaire:

Enregistrer un commentaire