2013-10-30 34 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員工!