【发布时间】:2013-05-01 21:52:58
【问题描述】:
我正在编写一个单元测试,我偶然发现了一个非常奇怪的行为。
我有一个测试夹具,所以我希望在数据库中看到 1 行(夹具中只列出了 1 行)我编写了一些代码来运行 findByAttributes,如下所示:
$client_id = 6;
$unique = array (
"client_id" => (String) $client_id,
"id" => "2" //I have a fixture that uses id 1
);
$model = Agent::model()->findByAttributes($unique);
die();
当我在测试运行后进行此配置并击中骰子时,我在数据库中看到 2 行。但是,如果我这样做:
$client_id = 6;
$unique = array (
"client_id" => (String) $client_id,
"id" => "2" //I have a fixture that uses id 1
);
die();
$model = Agent::model()->findByAttributes($unique);
//NOTE: I only moved the die() above the findByAttributes.
我只看到我的灯具的第一行。为什么 findByAttributes 在我的测试数据库中创建一行?
【问题讨论】:
-
不指定 'id' 会发生什么?我想这与此有关,但我现在无法运行任何测试。无论哪种方式,这都不应该在您的数据库中插入任何记录。是否有任何其他代码、固定装置或测试与此代码同时运行?
-
没有区别,我得到完全相同的行为。
-
您能否向我们展示您对该 CDbTestCase 的其余测试?我不认为这是问题所在。编辑:刚刚看到你关于移动 die() 语句的最后一点。我看不出这个模型为什么会插入一条记录。 $model 插入记录后的内容是什么?
-
这只是一个标准的 Yii 模型,没有花哨的东西。
-
$model 是一个有效的“Agent”Yii 模型,除了上面代码中的 $unique 标准之外,它还具有来自数据库的默认值。