采用Codeception的Laravel 4模型单元测试-类';雄辩的';找不到
我正在尝试使用来运行我的单元测试 为没有依赖项的简单类运行测试可以正常工作。但当我实例化一个依赖于雄辩的模型时,我得到了一个致命的错误: PHP致命错误:在第4行的/var/www/project/app/models/Role.PHP中找不到类“elount” 单元测试:采用Codeception的Laravel 4模型单元测试-类';雄辩的';找不到,laravel,phpunit,laravel-4,eloquent,codeception,Laravel,Phpunit,Laravel 4,Eloquent,Codeception,我正在尝试使用来运行我的单元测试 为没有依赖项的简单类运行测试可以正常工作。但当我实例化一个依赖于雄辩的模型时,我得到了一个致命的错误: PHP致命错误:在第4行的/var/www/project/app/models/Role.PHP中找不到类“elount” 单元测试: <?php use Codeception\Util\Stub; class TestModel extends \Codeception\TestCase\Test { public function test
<?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');
}
}
我做错了什么?通过查看,我能够看到如何引导自动加载来解决此问题,方法是将这些行添加到project/app/tests/_boostrap.php:
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();
我可能会迟到,但如果你不需要codecept的东西。您应该扩展laravel的PHPUnit_Framework_测试用例实现,称为just TestCase。像这样:
class TestModel extends TestCase {
}
运行单元测试时,找不到雄辩的
类
尝试添加使用illumb\Database\elount\Model作为elount
到Role.php
您可以转到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);
}
这个问题的答案现在有点过时了。使用Laravel5时,我得到了相同的错误(找不到'elount'类…),并通过从Laravels库复制代码来解决它。此文件用于在Laravel框架内进行测试(不使用codeception)
要修复“Eloquent not found”错误,请将以下行添加到project/tests/unit/_bootstrap.php
<?php
$app = require __DIR__.'/../../bootstrap/app.php';
$app->make('Illuminate\Contracts\Console\Kernel')->bootstrap();
我通过在\u bootstrap.php
$app=require\uuuuu DIR\uuuu.'/../bootstrap/start.php';
$app->boot();
希望这有助于谷歌的同行 我不知道codeception,但对于PHPUnit,你至少需要知道。也许Codeception也是如此。我知道这太晚了,但是在你安装了composer软件包之后。您可以键入站点的根目录。“vendor/bin/codecept_bootstrap”,这将为您生成文件。请注意包含路径!
<?php
$app = require __DIR__.'/../../bootstrap/app.php';
$app->make('Illuminate\Contracts\Console\Kernel')->bootstrap();