Sytrus's avatar

Can't create test for Cache

Hi all! I am trying to create a test, which will make file cache and test if it exist. According to Laravel Docs I make this code:

public function testCache()
{
    Cache::shouldReceive('remember')
        ->once()
        ->with(md5(1), 120, Closure::class)
        ->andReturn('Closure');
}

But getting error:

		Method remember('c4ca4238a0b923820dcc509a6f75849b', 120, 'Closure') from 															 
        Mockery_0_Illuminate_Cache_CacheManager should be called exactly 1 times but called 0 times.

What am I doing wrong? Where to get docs about shouldReceive and what I must put to the andReturn ?

Thanks for responds!

0 likes
5 replies
Sinnbeck's avatar

I assume you aren't showing the complete code as your test does not do anything?

And show the code you are testing

Sytrus's avatar

@Sinnbeck this is whole code) Should I make some using of Cache before this code?

For example add:

$data = Cache::remember( md5(1), 4000, function () {
		return Http::get('/');
    });
Sytrus's avatar

@Sinnbeck now I getting "passed"

public function testCache()
{
    $key = md5(1);
    $duration = 120;

    $object = Cache::remember( $key, $duration, function () {
        return Carbon::today();
    });

    Cache::shouldReceive('get')
        ->with($key, $duration, \Closure::class)
        ->andReturn($object);
}

Is that mean I testd the cache exists? Thanks for your responds, I am laravel newbie :-)

Sinnbeck's avatar

@Sytrus yes but you don't actually test cache directly. Laravel does that already. You use the mock to test your own code in a controller or similar

Please or to participate in this conversation.