【发布时间】:2017-03-11 14:43:49
【问题描述】:
我正在为 JobMapper 编写测试,我得到的响应是:
ReflectionException: Class config does not exist。虽然我已经追踪了这个问题,但我似乎没有找到任何解决方案。
我的一个假设是因为在这一行中:
$this->eloquentJobMapper->toEloquent($validJobModel)
我正在调用数据库,当前的$app实例没有正确初始化,因此无法访问数据库。
关于这个有什么想法吗?
use \Illuminate\Foundation\Testing\TestCase;
use MNF\MJ\Models\Job;
use MNF\MJ\Mapper\EloquentJobMapper;
use MNF\MJ\Repositories\EloquentJobRepository;
use \Illuminate\Support\Facades\Facade as Facade;
use \Illuminate\Container\Container as Container;
use MNF\MJ\Persistence\EloquentJob;
use \Illuminate\Foundation\Application;
class JobMapperTest extends TestCase
{
/**
* @var Job
*/
protected $jobMock;
/**
* @var EloquentJobRepository
*/
protected $eloquentJobRepositoryMock;
/**
* @var EloquentJobMapper $eloquentJobMapper
*/
protected $eloquentJobMapper;
/**
* JobMapperTest setUp().
*/
public function setUp()
{
parent::setUp();
$this->jobMock = Mockery::mock('MNF\MJ\Models\Job');
$this->eloquentJobRepositoryMock = Mockery::mock('MNF\MJ\Repositories\EloquentJobRepository');
$this->eloquentJobMapper = new EloquentJobMapper();
$app = new Container();
$app->singleton('app', 'Illuminate\Container\Container');
$app->instance(Job::class, $this->jobMock);
Facade::setFacadeApplication($app);
}
public function tearDown()
{
parent::tearDown();
Mockery::close();
}
/**
* Creates the application.
*
* @return Symfony\Component\HttpKernel\HttpKernelInterface
*/
public function createApplication()
{
return new Application();
}
//1) giving a valid a Job model, I am getting back an Eloquent Job Model
public function testGivingValidMJModelGettingBackEloquentMJModel()
{
$validJobModel = new Job();
$validJobModel->setJobID(23)
/// ..some setters
$eloquentJob = $this->eloquentJobMapper->toEloquent($validJobModel);
$this->assertInstanceOf(EloquentJob::class, $eloquentJob);
}
}
【问题讨论】:
-
在 Laravel 5 上,这个错误意味着你的类自动加载没有加载。对于现在遇到这种情况的任何人,他们可能想查看他们的测试引导代码。
标签: php laravel laravel-4 phpunit mockery