【问题标题】:Reflection Exception Class filesystem does not exist反射异常类文件系统不存在
【发布时间】:2016-10-25 15:21:00
【问题描述】:

我正在为使用 Laravel 5 中的 Storage 外观的类创建测试。运行测试时出现错误“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')”引起的

【问题讨论】:

    标签: php laravel-5 phpunit


    【解决方案1】:

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

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

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

    【讨论】:

      【解决方案2】:

      您可以将函数修改为:

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

      【讨论】:

      • 我试过了,但仍然得到同样的错误原因是由 mockStorage 函数中的 Storage::extend 引起的。
      猜你喜欢
      • 2016-12-07
      • 2018-04-22
      • 2017-08-16
      • 1970-01-01
      • 2019-05-26
      • 2020-01-03
      • 2019-05-11
      • 2012-11-05
      • 1970-01-01
      相关资源
      最近更新 更多