Laravel facade class not found when taunted in unit test

I might be missing something, but I have a very simple helper class that creates a directory:

// Helper class <?php namespace MyApp\Helpers; use User; use File; class FileSystemHelper { protected $userBin = 'users/uploads'; public function createUserUploadBin(User $user) { $path = $this->userBin . '/' . $user->id; if ( ! File::isDirectory($path)) { File::makeDirectory($path); } } } 

And the related test is here:

 // Associated test class <?php use MyApp\Helpers\FileSystemHelper; class FileSystemHelperTest extends TestCase { protected $fileSystemHelper; public function setUp() { $this->fileSystemHelper = new FileSystemHelper; } public function testNewUploadBinCreatedWhenNotExists() { $user = new User; // this would be mocked File::shouldReceive('makeDirectory')->once(); $this->fileSystemHelper->createUserUploadBin($user); } } 

However, when I run the test, I get a fatal error:

PHP Fatal error: Class 'File' not found in /my/app/folder/app/tests/lib/myapp/helpers/FileSystemHelperTest.php

I looked through the documents for a mockery of the facade, and I do not see where I am mistaken. Any suggestions?

thanks

+7
source share
2 answers

I missed this in the docs:

Note. If you define your own setUp method, be sure to call parent :: setUp.

The challenge that cured the problem. Doh!

+20
source

because the laravel structure does not load before using the facade OR, because you do not use the laravel php unit (TestCase class), here is an example of the verification code that is in the application // attention to continue from TestCase not ()

 /** * TEST CASE the application. * */ class TestCase extends Illuminate\Foundation\Testing\TestCase { /** * Creates the application. * * @return \Symfony\Component\HttpKernel\HttpKernelInterface */ public function createApplication() { $unitTesting = true; $testEnvironment = 'testing'; //this line boot the laravel framework so all facades are in your hand return require __DIR__.'/../../bootstrap/start.php'; } } 

code>

+3
source

All Articles