【问题标题】:Laravel 4 model unit test with Codeception - Class 'Eloquent' not found带有 Codeception 的 Laravel 4 模型单元测试 - 未找到“Eloquent”类
【发布时间】:2013-11-09 19:35:37
【问题描述】:

我正在尝试使用 Codeception 运行我的 Laravel 4 单元测试。

为没有依赖关系的简单类运行测试可以正常工作。但是当我实例化一个依赖于 Eloquent 的模型时,我得到了这个致命错误:

PHP 致命错误:在第 4 行的 /var/www/project/app/models/Role.php 中找不到“Eloquent”类

单元测试:

<?php
use Codeception\Util\Stub;
class TestModel extends \Codeception\TestCase\Test 
{
  public function testExample()
  {
    $role = new Role;
    $role->name = 'superuser';
    $this->assertEquals('superuser', $role->name);
  }
}

型号:

<?php
class Role extends Eloquent
{
  public function users()
  {
    return $this->belongsToMany('User');
  }
}

项目结构:

我正在从项目根目录运行 vendor/bin/codecept 运行单元,文件结构如下:

/project
  codeception.yml
  /vendor/bin/codecept
  /app
    /tests
      unit.suite.yml
      /unit
         ExampleTest.php
    /models
       Role.php
                ...etc

我做错了什么?

【问题讨论】:

  • 我特别不知道代码接收,但是对于 PHPUnit,你至少需要bootstrap the class autoloading。或许 Codeception 也是如此。
  • 我知道这太晚了,但是在你安装了 composer 包之后。您输入站点的根目录。 “vendor/bin/codecept _bootstrap”,它将为您生成文件。

标签: laravel phpunit laravel-4 eloquent codeception


【解决方案1】:

通过查看Codeception L4 sample app,我可以通过将这些行添加到 project/app/tests/_boostrap.php 来了解如何引导自动加载来解决此问题:: p>

include __DIR__.'/../../vendor/autoload.php';
$app = require_once __DIR__.'/../../bootstrap/start.php';
\Codeception\Util\Autoload::registerSuffix('Page', __DIR__.DIRECTORY_SEPARATOR.'_pages');

编辑:从 Laravel 4.0 升级到 4.1 时,还需要多加一行:

$app->boot();

【讨论】:

  • 小心包含路径!
【解决方案2】:

我可能迟到了,但如果你不需要密码学的东西。您应该扩展 laravel 的 PHPUnit_Framework_TestCase 实现,称为 TestCase。像这样:

class TestModel extends TestCase {
}

【讨论】:

    【解决方案3】:

    这个问题的答案现在有点过时了。使用 Laravel 5 我遇到了同样的错误(找不到类 'Eloquent'...)并通过从 Laravel 库 TestCase.php file 复制代码来解决它。该文件用于在 Laravel 框架内进行测试(不使用 codeception)。

    要修复“找不到 Eloquent”错误,请将以下行添加到 project/tests/unit/_bootstrap.php

    <?php
    $app = require __DIR__.'/../../bootstrap/app.php';
    $app->make('Illuminate\Contracts\Console\Kernel')->bootstrap();
    

    老实说,我不确定它为什么有效,但确实有效!如果我找出原因或有人 cmets,我会进行编辑。

    【讨论】:

      【解决方案4】:

      运行单元测试时找不到Eloquent 类。

      尝试将use Illuminate\Database\Eloquent\Model as Eloquent; 添加到Role.php

      【讨论】:

        【解决方案5】:

        您可以转到 TestCase 类并通过添加 auth 或一些来覆盖方法 refreshApplication(向 TestCase 添加方法):

        protected function refreshApplication()
        {
            $this->app = $this->createApplication();
        
            $this->client = $this->createClient();
        
            $this->app->setRequestForConsoleEnvironment();
        
            $this->app->boot();
        
            // authenticate your user here, when app is ready
            $user = new User(array('username' => 'John', 'password' => 'test'));
            $this->be($user);
        }
        

        【讨论】:

          【解决方案6】:

          我通过将以下几行添加到 _bootstrap.php 解决了 Laravel 4 和 Codeception 的类似问题

          $app = require __DIR__.'/../../bootstrap/start.php'; $app->boot();

          希望这对谷歌同事有所帮助!

          【讨论】:

            猜你喜欢
            • 2015-06-25
            • 2018-12-27
            • 2016-03-16
            • 2012-12-26
            • 2018-08-05
            • 2013-06-06
            • 1970-01-01
            • 1970-01-01
            • 2013-04-24
            相关资源
            最近更新 更多