【问题标题】:Eloquent class not found when testing new models测试新模型时找不到 Eloquent 类
【发布时间】:2013-06-06 08:31:23
【问题描述】:

我正在尝试测试我的 eloquent 模型,但我的测试一直失败,并出现“未找到 Class 'Eloquent'”错误。如果我添加一个使用我的 eloquent 模型的路由并简单地打印存储在数据库中的一些信息,那么一切正常。只有在尝试运行 phpunit 时,我才会遇到 eloquent 未找到的问题。我的模型在 app/models 中,所以它应该包含在 composer classmap 中,我已经完成了composer dump-autoload。我确定我忽略了一些非常明显的东西,但我无法挑选出来。知道问题是什么吗?

我的测试:

class GameTest extends TestCase {

    public function setUp(){
        $this->game = Game::find(1);
    }

    public function testGameInstance(){
        $this->assertInstanceOf('Game', $this->game);
    }
}

我的模特:

class Game extends Eloquent{

    protected $table = 'gm_game';
    protected $primaryKey = 'game_id';
}

【问题讨论】:

    标签: phpunit laravel laravel-4 eloquent


    【解决方案1】:

    尝试在测试的 setUp 函数中添加 parent::setUp()。这为我解决了这个问题。

    例子:

    class GameTest extends TestCase {
    
        public function setUp(){
           parent::SetUp();
           $this->game = Game::find(1);
        }
    
        public function testGameInstance(){
            $this->assertInstanceOf('Game', $this->game);
        }
    }
    

    【讨论】:

    猜你喜欢
    • 2013-11-09
    • 2013-04-24
    • 1970-01-01
    • 1970-01-01
    • 2014-02-15
    • 1970-01-01
    • 2015-06-25
    • 2019-03-30
    • 2018-08-16
    相关资源
    最近更新 更多