【发布时间】:2017-04-11 15:39:13
【问题描述】:
我使用 Codeception 为我的 Yii2 应用程序编写了一个简单的测试。我不想使用真正的 MySQL 数据库,而是使用固定装置。
代码如下:
测试/PersonTest.php:
namespace app\tests\unit\models;
use tests\fixtures;
use app\controllers;
class PersonTest extends \Codeception\Test\Unit
{
protected $tester;
public $appConfig = '@app/config/main.php';
protected function _before(){ }
protected function _after(){ }
public function _fixtures()
{
return [ 'Person' => fixtures\PersonFixture::className() ];
}
public function testUser(){
$person = Person::findOne( [ "id" => 1 ] );
$userId = isset( $person->id ) ? $person->id : false;
$this->assertEquals( 1, $userId );
}
}
tests/fixtures/data/Person.php
return [
'person1' => [
'id' => 1,
'firstname' => 'Foo',
'lastname' => 'Bar',
],
];
测试/夹具/Person.php
namespace tests\fixtures;
use yii\test\ActiveFixture;
class PersonFixture extends ActiveFixture
{
public $modelClass = 'app\models\Person';
}
当我运行测试时,我得到了错误:
[错误] 找不到类“tests\fixtures\PersonFixture”
我尝试了 100 种不同的方法,但我无法成功。如果这个简单的例子对我有用,我可以创建真正的测试。
【问题讨论】:
标签: unit-testing yii yii2 codeception fixtures