Phpunit 在单元测试中模拟时未找到Laravel facade类
我可能在这里遗漏了一些东西,但我有一个非常简单的帮助器类,它创建了一个目录:Phpunit 在单元测试中模拟时未找到Laravel facade类,phpunit,laravel,laravel-4,mockery,Phpunit,Laravel,Laravel 4,Mockery,我可能在这里遗漏了一些东西,但我有一个非常简单的帮助器类,它创建了一个目录: // Helper class <?php namespace MyApp\Helpers; use User; use File; class FileSystemHelper { protected $userBin = 'users/uploads'; public function createUserUploadBin(User $
// 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);
}
}
}
//助手类
我在文档中遗漏了这一点:
注意:如果您定义自己的安装方法,请务必调用parent::setUp
打电话解决了这个问题。啊 这是因为在使用facade之前没有加载laravel框架,或者是因为没有使用laravel php单元(TestCase类)
下面是一个示例代码,用于测试应用程序中的用例/
//注意扩展自TestCase而非()
为我工作-谢谢!出于某种原因,我的代码扩展了PHPUnit_Framework_测试用例,而不是使用Laravel 5.3(是的,我知道,是旧版本)的Illumb版本:/Valid answer。
// 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);
}
}
/**
* 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';
}
}