【问题标题】:ReflectionException: Class config does not existReflectionException:类配置不存在
【发布时间】: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


【解决方案1】:

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

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

这样你可能使容器没有配置类集。而不是这个可能你应该使用:

\App::instance(Job::class, $this->jobMock);

应该够了。

【讨论】:

  • 非常感谢您的回复。不幸的是,我遇到了同样的错误。这可能与它是 Laravel 4.2 安装有关吗?
  • 你没有提到它是 Laravel 4.2。真的很难说。您可能在其他地方使用了配置,但您没有显示代码
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-12-11
  • 2017-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-03
  • 1970-01-01
相关资源
最近更新 更多