Простой unit-Test модели в Yii2 проверяющий поля, обязательные для заполнения, и уникальность поля name
. При запуске с tearDown()
тест не завершается, выходит без ошибок.
D:\OpenServer\domains\localhost\yii2apps\consult\tests>codecept run codeception\unit\models\GroupTest.php Codeception PHP Testing Framework v2.1.5 Powered by PHPUnit 4.8.21 by Sebastian Bergmann and contributors.
Unit Tests (2) -------------------------------------------------------------------------------------------------- Trying to test empty attributes (codeception\unit\models\GroupTest::testEmptyAttributes)... D:\OpenServer\domains\localhost\yii2apps\consult\tests>
Если вместо tearDown
использовать setUp()
, то тест успешно завершает работу
D:\OpenServer\domains\localhost\yii2apps\consult\tests>codecept run codeception\unit\models\GroupTest.php Codeception PHP Testing Framework v2.1.5 Powered by PHPUnit 4.8.21 by Sebastian Bergmann and contributors.
Unit Tests (2) -------------------------------------------------------------------------------------------------- Test empty attributes (codeception\unit\models\GroupTest::testEmptyAttributes) Test unique
name (codeception\unit\models\GroupTest::testUniqueName)
Time: 832 ms, Memory: 11.00Mb
OK (2 tests, 6 assertions)
D:\OpenServer\domains\localhost\yii2apps\consult\tests>
Сам тест:
<?php
namespace codeception\unit\models;
use app\models\Group;
use yii\codeception\TestCase;
class GroupTest extends TestCase
{
protected function tearDown()
{
parent::tearDown();
Group::deleteAll();
}
public function testEmptyAttributes(){
$model = new Group();
$this->assertFalse($model->validate(),'validation failed');
$this->assertArrayHasKey('name',$model->getErrors(),'check existed name');
}
public function testUniqueName(){
$model = new Group(['name'=>'testGroup']);
$newModel = new Group(['name'=>'testGroup']);
$this->assertTrue($model->validate(),'model validated');
$this->assertTrue($model->save(),'model saved');
$this->assertFalse($newModel->validate(),'model validation failed');
$this->assertArrayHasKey('name',$newModel->getErrors(),'check unique name');
}
}