【发布时间】:2015-06-24 14:50:34
【问题描述】:
我正在尝试测试一个旨在进行一些数据库维护的 artisan 命令。
特别是,它会搜索没有填满列的记录,然后将其填满。
这是命令的fire()方法的简化版:
public function fire()
{
$items = Item::all();
$total = $items->count();
$updated = 0;
foreach ($items as $item) {
if ($item->myColumn != 'x') {
$item->myColumn = 'x';
$item->save();
$updated++;
}
}
$this->info("total: $total updated: $updated");
}
我的(验收)测试非常简单,并且执行以下操作:
- 在数据库中插入一条记录
- 调用
artisan命令 - 检查插入的记录是否已更新
这是代码:
public function doTheTest(AcceptanceTester $I)
{
$I->wantTo('setup the myColumn when it is not set');
$id = $I->haveRecord('items', [
'myColumn' => '',
]);
$I->runShellCommand('php artisan items:updater');
$I->seeRecord('items', [
'id' => $id,
'myColumn' => 'x',
]);
}
但是测试失败,我收到以下消息:
Couldn't see record "items",{"id":101,"myColumn":"x"}:
Couldn't find items with {"id":101,"code":"x"}
如您所见,新记录的id是101,因为db转储中已经有100个项目,但奇怪的是命令中的$this->info()打印出来
total: 100 updated: 100
好像测试中使用的数据库和artisan中使用的数据库不同。
另外,如果在测试结束时我尝试抓取添加的记录,并打印出来,如下面的sn-p
public function doTheTest(AcceptanceTester $I)
{
/* ... */
$item = $I->grabRecord('items', [
'id' => $id,
]);
\Codeception\Util\Debug::debug($item);
}
然后运行codecept run acceptance --debug 命令,我得到了添加的记录
stdClass Object
(
[id] => 101
[myColumn] =>
)
我很困惑,因为只有一个数据库,但我肯定误解了这里的重要内容。
谁能帮帮我?
非常感谢,
【问题讨论】:
标签: php database laravel codeception laravel-artisan