Chr*_*lke 18 postgresql unit-testing cakephp cakephp-3.0
我正在使用Postgres(我认为与问题有关)和CakePHP 3.
我有以下单元测试,只是检查以确保模型可以保存有效的数据集.当我运行以下测试时,使用标准的"bake'd"模型单元测试,我得到以下错误.
我认为这是问题所在:
我们正在使用灯具来添加一些基础数据.这是我认为可能导致问题的唯一地方.为此添加信任,在单元测试运行时,我运行以下命令以获取下一个自动递增id值,并返回1,即使它在非测试DB中返回了正确的数字.Select nextval(pg_get_serial_sequence('agencies', 'id')) as new_id;
单元测试:
public function testValidationDefault()
{
$agencyData = [
'full_name' => 'Agency Full Name',
'mode' => 'transit',
'request_api_class' => 'Rest\Get\Json',
'response_api_class' => 'NextBus\Generic',
'realtime_url_pattern' => 'http://api.example.com',
'routes' => '{"123": {"full_route": "123 Full Route", "route_color": "#123456"}}'
];
$agency = $this->Agencies->newEntity($agencyData);
$saved = $this->Agencies->save($agency);
$this->assertInstanceOf('App\Model\Entity\Agency', $saved);
}
Run Code Online (Sandbox Code Playgroud)
错误:
PDOException: SQLSTATE[23505]: Unique violation: 7 ERROR: duplicate key value violates unique constraint "agencies_pkey"
DETAIL: Key (id)=(1) already exists.
Run Code Online (Sandbox Code Playgroud)
我尝试过的事情
这个夹具确实为每个记录设置了ID字段.从夹具中删除它们确实有效,但它会破坏依赖于某些关系数据的其他单元测试.
我不喜欢这个解决方案,但在保存实体之前添加以下内容确实有效。
$this->Agencies->deleteAll('1=1');
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
563 次 |
| 最近记录: |