nexxai's avatar
Level 37

How to Storage::fake() an entire set of tests

I have a class that an uploaded file is required and so I've added the UploadedFile::fake() to the factory, but I really don't want to be uploading actual files to the host for every test (there are a lot of tests) and so I want to mock the Storage. The Laravel docs say to use "Storage::fake()" which makes sense but I don't want to have to add that to every test and so I was hoping to use it in the __construct method like:

<?php

namespace Tests\Feature;

use Tests\TestCase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Illuminate\Foundation\Testing\RefreshDatabase;

class StudioTest extends TestCase
{
    use RefreshDatabase;

    public function __construct()
    {
        Storage::fake('storage_location');
    }

but when I do, I get this error:

PHP Fatal error:  Uncaught ReflectionException: Class path.storage does not exist in /Users/path/vendor/laravel/framework/src/Illuminate/Container/Container.php:790
Stack trace:
#0 /Users/path/vendor/laravel/framework/src/Illuminate/Container/Container.php(790): ReflectionClass->__construct('path.storage')
#1 /Users/path/vendor/laravel/framework/src/Illuminate/Container/Container.php(667): Illuminate\Container\Container->build('path.storage')
#2 /Users/path/vendor/laravel/framework/src/Illuminate/Container/Container.php(615): Illuminate\Container\Container->resolve('path.storage', Array)
#3 /Users/path/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php(121): Illuminate\Container\Container->make('path.storage', Array)
#4 /Users/path/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php(846): app('path.storage')
#5 /Users/path/vendor/laravel/framework/src/Illuminate/Support/Facades/Storage.php(26): in /Users/path/vendor/laravel/framework/src/Illuminate/Container/Container.php on line 790

Fatal error: Uncaught ReflectionException: Class path.storage does not exist in /Users/path/vendor/laravel/framework/src/Illuminate/Container/Container.php:790
Stack trace:
#0 /Users/path/vendor/laravel/framework/src/Illuminate/Container/Container.php(790): ReflectionClass->__construct('path.storage')
#1 /Users/path/vendor/laravel/framework/src/Illuminate/Container/Container.php(667): Illuminate\Container\Container->build('path.storage')
#2 /Users/path/vendor/laravel/framework/src/Illuminate/Container/Container.php(615): Illuminate\Container\Container->resolve('path.storage', Array)
#3 /Users/path/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php(121): Illuminate\Container\Container->make('path.storage', Array)
#4 /Users/path/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php(846): app('path.storage')
#5 /Users/path/vendor/laravel/framework/src/Illuminate/Support/Facades/Storage.php(26): in /Users/path/vendor/laravel/framework/src/Illuminate/Container/Container.php on line 790

which I assume has something to do with the fact that Storage:: is just a facade, but I don't know enough PHP to know what to do about it or even the right terms to Google.

Can anyone help me out?

0 likes
3 replies
mware's avatar
mware
Best Answer
Level 9

You can add a setUp function at the beginning for your test class that will be called before every test.

use RefreshDatabase;

public function setUp()
{
    parent::setUp();
    Storage::fake('storage_location');
}

You can then get rid of the constructor.

1 like
nexxai's avatar
Level 37

Ugh, I knew that. I'm an idiot.

Thanks!

1 like

Please or to participate in this conversation.