如何在Yii2 Codeception中重用测试

时间:2015-02-26 13:00:32

标签: unit-testing yii2 code-reuse codeception

我正在基于Yii2和Codeception设置新项目。我正在使用高级应用程序模板(后端,常见,前端)。

我希望大多数前端ActiveRecords都是“ReadOnly”所以我制作了特殊的Trait,我阻止了适当的方法,如保存,插入,删除,更新......

trait ReadOnlyActiveRecord {

/**
 * @throws \yii\web\MethodNotAllowedHttpException
 */
public function save($runValidation = true, $attributeNames = null)
{
    return self::throwNotAllowedException(__FUNCTION__);
}

//...

它只是抛出一个MethodNotAllowedHttpException。

现在我在多个前端AR中使用此Trait并希望使用Codeception测试它们,如下所示

use \Codeception\Specify;

/**
 * @expectedException \yii\web\MethodNotAllowedHttpException
 */
public function testSaveMethod()
{
    $model = new AppLanguage();

    $this->specify('model should be unsaveable', function () use ($model) {

        expect('save function is not allowed', $model->save())->false();
    });
}

/**
 * @expectedException \yii\web\MethodNotAllowedHttpException
 */
public function testInsertMethod()
{
    $model = new AppLanguage();

    $this->specify('model should be unisertable', function () use ($model) {

        expect('insert function is not allowed', $model->save())->false();
    });
}

// ...

我现在正在弄清楚如何在多个TestCests中使用这些测试,因此我不会在每个Cest中反复重写代码。

所以我在想像

这样的东西
/**
 * Tests ActiveRecord is read only
 */
public function testReadOnly()
{
    $model = new AppLanguage();

    $this->processReadOnlyTests($model);
}

所以我的问题是:

在哪里放置测试方法以及如何在特定的Cests中包含和调用它们?

有什么建议吗?

谢谢。

0 个答案:

没有答案
相关问题