ReflectionException:类配置不存在

时间:2016-10-28 16:45:46

标签: php laravel laravel-4 phpunit mockery

我正在为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);

}

}

1 个答案:

答案 0 :(得分:0)

我可能错了,但我完全不明白为什么你在这里创建新的容器对象:

$app = new Container();
$app->singleton('app', 'Illuminate\Container\Container');
$app->instance(Job::class, $this->jobMock);
Facade::setFacadeApplication($app);

这种方式可能是你使容器没有设置配置类。可能你应该使用:

而不是这个
\App::instance(Job::class, $this->jobMock);

它应该足够了。

相关问题