2017-04-11 6 views
1

私はCodeceptionを使って私のYii2アプリケーションのための簡単なテストを書いた。実際のMySQLデータベースを使用する代わりに、私は什器を使いたいです。ここYii2 + Codeception:治具の使い方は?

コードである:/ 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); 
    } 
} 

テスト/器具/データ/ 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'; 
} 

[エラー]クラスの備品\テストを\ PersonFixture「私は100の異なるものを試してみました

見つけましたが、私はできませんそれを働かせないでください。この単純な例が私のために働くならば、私は本当のテストを作ることができます。

答えて

-1

yii2-codeceptionエクステンションを使用する必要があります。これは、フィクスチャを自動的にロードします。

インストール後に、yii\codeception\DbTestCaseクラスを使用可能にする場合は、PersonTestを拡張する必要があります。

Person fixtureには、次のような名前空間が必要です。app\tests\fixtures。 Codeceptionで

+3

yii2-codeception拡張は推奨されていません:https://github.com/yiisoft/yii2-codeception – yesnik

0

は、あなたがこのようにそれを行うことができます2.3.8:

はあなたのフィクスチャを定義します(そして、あなたの疑問を持っているだけのようデータファイルを持っている)

namespace app\tests\fixtures; 

class PersonFixture extends \yii\test\ActiveFixture { 

    public $modelClass = 'app\models\Person'; 

} 

そして、あなたのテスト

を書きます
namespace app\tests\unit; 

class PersonTest extends \Codeception\Test\Unit { 

    public function _fixtures() { 
     return [ 
      'persons' => 'app\tests\fixtures\PersonFixture', 
     ]; 
    } 

    public function testUser() { 
     $person1 = $this->tester->grabFixture('persons', 'person1'); 
     $this->assertEquals(1, $person1->id); 
    } 

} 

これだけです。

関連する問題