导入类以测试模型

时间:2019-03-16 10:45:34

标签: php unit-testing yii2 phpunit yii2-basic-app

我在dir / tests中创建UnitTest类

StationTest Hierachy

现在,我想在测试Station中使用类StationsTest.php

namespace app\Test;
namespace app\modules\admin\controllers;

use Codeception\PHPUnit\TestCase;
use app\models\Station;

class StationsTest extends TestCase
{
    public function testCreateStation()
    {
        $station = new Station();
        $station->name = 'TestName';
        $station->save();
    }
}

但是当我运行测试时:

vendor\bin\phpunit --verbose tests\StationsTest

我遇到错误

Error: Class 'app\models\Station' not found

如何解决?

1 个答案:

答案 0 :(得分:0)

确保在启动测试之前运行以下命令:

composer dump-autoload

,并且您的phpunit.xml.dist具有这样的条目:

<?xml version="1.0"?>
<phpunit
        bootstrap="./vendor/autoload.php"

        [..] other properties
>
[..]
</phpunit>