0

In my laravel application, I use Redis to store some cache (e.g. the list of items to show on the front page). I always access Redis through the Facade: Illuminate\Support\Facades\Redis.

I created a different Redis database for testing (1 instead of 0), but I also need to reset it after each test, so that the test never gets data from a previous test.

Is there an efficient way to create this behaviour?

I tried to implement it using the @before annotation:

/**
 * @before
 */
public function prepareForTesting() {
    Redis::flushdb();
}

But I get the error: Cannot use 'FLUSHDB' over clusters of connections.

Any ideas?

gbalduzzi
  • 9,356
  • 28
  • 58
  • 2
    Are you unable to use the `Cache` facade ? If it's only for caching this is the recommended approach and in tests the default cache driver is an array so always gets reset between tests – apokryfos Apr 08 '19 at 13:15
  • I managed to refactor my code and it worked, thanks! – gbalduzzi Apr 08 '19 at 14:04

1 Answers1

0

Maybe you could use the built in artisan cache:clear command?

Like this:

/**
 * @before
 */
public function prepareForTesting() {
    Artisan::call('cache:clear');
}
Niklas
  • 1,729
  • 1
  • 12
  • 19