2013-10-30 33 views
5

我试图用Codeception来运行我的Laravel 4单元测试。Laravel与Codeception的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'); 
    } 
} 

项目结构:

我是从项目的根目录运行供应商/斌/ codecept运行单元,这个文件结构:

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

我在做什么错?

+1

我不太了解codeception,但是对于PHPUnit,您至少需要[引导类自动加载](https://github.com/laravel/laravel/blob/master/phpunit.xml#L4)。也许这与Codeception是一样的。 – fideloper

+0

我知道这是疯了,但后安装composer包。您输入该网站的根目录。 “vendor/bin/codecept _bootstrap”并且会为你生成文件。 – John

回答

8

通过查看Codeception L4 sample app,我能看到如何来引导自动加载到解决此问题,通过将这些线项目/应用/测试/ _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(); 
+0

要小心,包括PATH! – Chung

3

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

class TestModel extends TestCase { 
} 
0

Eloquent类,当你运行你的单元测试无法找到。

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

0

你可以去TestCase类和重写方法refreshApplication(添加方法的TestCase)与添加auth或一些:

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); 
} 
3

回答这个问题现在是有点过时。随着Laravel 5我得到了同样的错误(类'雄辩'未找到...)并通过复制代码从Laravels基地TestCase.php file解决它。该文件用于在Laravel框架内进行测试(不使用代码)。

要修复'找不到的雄辩'错误,请将以下行添加到project/tests/unit/_bootstrap。php

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

老实说,我不知道它为什么可以工作,但它确实!如果我找出为什么或有人评论,我会编辑。

0

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

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

希望这有助于一个老乡Google员工!