反射异常 Class 文件系统不存在
Reflection Exception Class filesystem does not exist
我正在为使用 Laravel 5 中的 Storage facade 的 class 创建一个测试。当我 运行 测试时,我收到错误 "ReflectionException: Class filesystem does not exist"。
这是测试代码
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\Storage as Storage;
use App\Fourpoint\services\Material;
use \Illuminate\Container\Container as Container;
use \Illuminate\Support\Facades\Facade as Facade;
class MaterialServiceTest extends TestCase
{
use DatabaseMigrations;
protected $directory;
protected $file;
public function setUp(){
$app = new Container();
$app->singleton('app', 'Illuminate\Container\Container');
Facade::setFacadeApplication($app);
$this -> mat = new \App\FourPoint\services\Material();
$this -> directory = 'home/Code/project4point/tests/mockFiles';
$this -> file = new \Symfony\Component\HttpFoundation\File\UploadedFile('tests/mockFiles/testPDF.pdf', 'testPDF.pdf');
}
public function testNewMaterial(){
Storage::shouldReceive('put')
->andReturn(true);
Storage::shouldReceive('exists')
->andReturn(true);
$this -> mat->newMaterial('pdf',1,1,'testPDF',$this -> file,1,1);
}
}
错误是由"Storage::shouldReceive('put')"
引起的
您可以将函数修改为:
public function testNewMaterial()
{
$storage = $this->mockStorage();
$storage->shouldReceive('put')->andReturn(true);
$storage->shouldReceive('exists')->andReturn(true);
$this->mat->newMaterial('pdf',1,1,'testPDF',$this -> file,1,1);
}
protected function mockStorage()
{
Storage::extend('mock', function() {
return \Mockery::mock(\Illuminate\Contracts\Filesystem\Filesystem::class);
});
Config::set('filesystems.disks.mock', ['driver' => 'mock']);
Config::set('filesystems.default', 'mock');
return Storage::disk();
}
这是因为您的 class 是从 testCase 扩展而来的,它已经有自己的方法 "setUp",应该是 运行。当您在子 classes 中定义方法 "setUp" 时,您将覆盖父 class 具有的方法。
解决方法:像这样先调用父class方法
public function setUp(){
parent::setUp();
... your own setup
}
我正在为使用 Laravel 5 中的 Storage facade 的 class 创建一个测试。当我 运行 测试时,我收到错误 "ReflectionException: Class filesystem does not exist"。
这是测试代码
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\Storage as Storage;
use App\Fourpoint\services\Material;
use \Illuminate\Container\Container as Container;
use \Illuminate\Support\Facades\Facade as Facade;
class MaterialServiceTest extends TestCase
{
use DatabaseMigrations;
protected $directory;
protected $file;
public function setUp(){
$app = new Container();
$app->singleton('app', 'Illuminate\Container\Container');
Facade::setFacadeApplication($app);
$this -> mat = new \App\FourPoint\services\Material();
$this -> directory = 'home/Code/project4point/tests/mockFiles';
$this -> file = new \Symfony\Component\HttpFoundation\File\UploadedFile('tests/mockFiles/testPDF.pdf', 'testPDF.pdf');
}
public function testNewMaterial(){
Storage::shouldReceive('put')
->andReturn(true);
Storage::shouldReceive('exists')
->andReturn(true);
$this -> mat->newMaterial('pdf',1,1,'testPDF',$this -> file,1,1);
}
}
错误是由"Storage::shouldReceive('put')"
引起的您可以将函数修改为:
public function testNewMaterial()
{
$storage = $this->mockStorage();
$storage->shouldReceive('put')->andReturn(true);
$storage->shouldReceive('exists')->andReturn(true);
$this->mat->newMaterial('pdf',1,1,'testPDF',$this -> file,1,1);
}
protected function mockStorage()
{
Storage::extend('mock', function() {
return \Mockery::mock(\Illuminate\Contracts\Filesystem\Filesystem::class);
});
Config::set('filesystems.disks.mock', ['driver' => 'mock']);
Config::set('filesystems.default', 'mock');
return Storage::disk();
}
这是因为您的 class 是从 testCase 扩展而来的,它已经有自己的方法 "setUp",应该是 运行。当您在子 classes 中定义方法 "setUp" 时,您将覆盖父 class 具有的方法。
解决方法:像这样先调用父class方法
public function setUp(){
parent::setUp();
... your own setup
}