【发布时间】:2017-01-06 19:48:08
【问题描述】:
我正在使用工厂进行单元测试,到目前为止我还没有遇到任何问题:
// This is the factory definition (note: other fields removed for brevity).
// Primary key is not included just like Laravel's docs show.
$factory->define(Media::class, function (Faker\Generator $faker) {
return [
'id_media' => $faker->unique()->randomNumber(6),
'type' => $faker->randomNumber(1)
// other irrelevant fields
];
});
// This is how I call it on my unit test
$count = 5;
$id = 3;
factory(Media::class, $count)->create([
'id_media' => $id
]);
问题是这会生成无效的 SQL(id_media 出现两次):
INSERT INTO media (id_media, type, id_media) VALUES (73052, 2)
如果我在单元测试中调用 id_media 属性时没有覆盖它,那么查询是有效的,但这不是我需要的。让我觉得奇怪的两件事是:
- 我在整个测试中都在使用它,但唯一一次生成无效 SQL 是在这个工厂中。我的应用程序中有大约两打工厂,这是唯一有问题的工厂。这个特定的表有一个主键,没有其他索引。这张桌子就像他们来的一样香草。
- 这实际上是在the Laravel docs 中推荐的。我没有做任何不寻常的事情。
【问题讨论】:
标签: php laravel unit-testing laravel-5.1