【问题标题】:Yii2 ActiveRecord Unit-testing with codeception.Yii2 ActiveRecord 带有代码接收的单元测试。
【发布时间】:2015-05-19 06:26:18
【问题描述】:

我正在尝试使用 yii2-codeception。我有一个扩展 ActiveRecord 的名为 Client 的类。

rules() 函数:

public function rules()
{
    return [
        [['user_agent', 'ip_address', 'language', 'city_slug'], 'required'],
        [['created_at', 'updated_at'], 'integer'],
        [['user_agent'], 'string', 'max' => 255],
        [['ip_address'], 'string', 'max' => 12],
        [['language'], 'string', 'max' => 16],
        [['city_slug'], 'string', 'max' => 32]
    ];
}

我在 tests\codeception\unit\models 文件夹中创建了一个名为 ClientTest 的文件,其内容为:

<?php

namespace tests\codeception\unit\models;

use Yii;
use yii\codeception\TestCase;
use Codeception\Specify;
use genesisc\clientForm\models\Client;
use Codeception\Util\Debug;

class ClientTest extends TestCase
{
    use Specify;

    public function testCreateWithoutAdditionalFields()
    {
        $this->specify("model must automaticaly create non-meta fields",      function() {
            $model = new Client;
            $x = $model->validate();
            Debug::debug($model->errors);die();
            $this->assertTrue($model->save(), 'Client saved without additional info');
        });

    }

}

当我使用 'codecept run unit --debug' 命令运行测试时,我看到 $model-&gt;errors 是空数组,但客户端模型具有必填字段。

【问题讨论】:

  • 我的代码有什么问题?
  • 您是否阅读过文档:yiiframework.com/doc-2.0/…。 “当且仅当属性是在场景()中声明的活动属性并且与在规则()中声明的一个或多个活动规则相关联时,才会验证属性。”
  • "scenes() 的默认实现将返回在验证规则声明方法 yii\base\Model::rules() 中找到的所有场景。
  • 我的代码和你的一样,为什么我会收到类似找不到类的错误,你能告诉我你的 unit.suite.yml

标签: unit-testing yii2


【解决方案1】:

好的。那是我的错。在客户端模型中有一个函数 beforeValidate(),它返回 null 值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-11
    • 1970-01-01
    • 1970-01-01
    • 2010-11-19
    • 2018-11-30
    • 2012-03-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多