反射异常类文件系统不存在

时间:2016-10-25 15:21:01

标签: php laravel-5 phpunit

我正在为使用Laravel 5中的存储外观的类创建测试。我在运行测试时收到错误“ReflectionException:Class filesystem not exists”。

以下是测试的代码     

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')”

引起的

2 个答案:

答案 0 :(得分:0)

您可以将您的功能修改为:

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();
}

答案 1 :(得分:0)

这是因为你的类从testCase扩展而来,它已经拥有了应该运行的自己的方法“setUp”。在子类中定义方法“setUp”时,将覆盖父类具有的方法。

解决方案:首先像这样调用父类方法

public function setUp(){
    parent::setUp();
    ... your own setup
}