由于某种原因,事务回滚在我的testCase中不起作用。当我从控制器执行相同的代码时,它会恢复执行良好的SQL。但是从我的测试用例执行时它不会回滚。
我已经尝试在测试数据库中禁用缓存并更改为我可以在我的服务器中使用的最新可能的cakephp版本,即2.6.13。但它没有奏效。 有人可以帮帮我吗?
示例:
TipoLog.php:
public function insert(){
$data = array(
$this->alias => array(
'id' => 99,
'descricao' => 'Testing 123',
),
);
$this->getDataSource()->begin();
$result = $this->save($data);
$this->getDataSource()->rollback(); // should undo $this->save
return $result;
}
在控制器上执行它时,效果很好:
MyController.php
public function teste() {
$this->autoRender = false;
$this->loadModel('ControleFrequencia.TipoLog');
$id = 99;
debug($this->TipoLog->findById($id)); //returns nothing - OK
$this->TipoLog->insert($id);
debug($this->TipoLog->findById($id)); //returns nothing - OK
}
但是当我执行testCase时,回滚不起作用:
MyTestCaseTest.php
public function testTest() {
$TipoLog = ClassRegistry::init('ControleFrequencia.TipoLog');
$id = 99;
debug($TipoLog->findById($id)); //returns nothing - OK
$TipoLog->insert($id);
debug($TipoLog->findById($id)); //returns the inserted row - NOT OK
}
testCase的SQL-LOG
...
192 BEGIN
193 SELECT COUNT(*) AS `count` FROM `my_db_test`.`tipo_log` AS `TipoLog` WHERE `TipoLog`.`id` = 99
194 SELECT COUNT(*) AS `count` FROM `my_db_test`.`tipo_log` AS `TipoLog` WHERE `TipoLog`.`id` = 99
195 SELECT COUNT(*) AS `count` FROM `my_db_test`.`tipo_log` AS `TipoLog` WHERE `TipoLog`.`descricao` = 'Testing 123' AND `TipoLog`.`id` != 99
196 SELECT COUNT(*) AS `count` FROM `my_db_test`.`tipo_log` AS `TipoLog` WHERE `TipoLog`.`id` = 99
197 SELECT COUNT(*) AS `count` FROM `my_db_test`.`tipo_log` AS `TipoLog` WHERE `TipoLog`.`id` = 99
198 INSERT INTO `my_db_test`.`tipo_log` (`id`, `descricao`) VALUES (99, 'Testing 123')
199 ROLLBACK
200 SELECT `TipoLog`.`id`, `TipoLog`.`descricao` FROM `my_db_test`.`tipo_log` AS `TipoLog` WHERE `TipoLog`.`id` = 99 LIMIT 1
database.php中
public $default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'myhost',
'login' => 'mylogin',
'password' => 'mypass',
'database' => 'my_db',
'prefix' => '',
);
public $test = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'myhost',
'login' => 'mylogin',
'password' => 'mypass',
'database' => 'my_db_test',
'prefix' => '',
);
答案 0 :(得分:0)
这是因为,默认情况下CakePHP灯具使用MEMORY
数据库引擎,它不支持交易功能。
https://dev.mysql.com/doc/refman/5.7/en/memory-storage-engine.html
您可以通过在测试中的某个位置运行debug($this->query("SHOW CREATE TABLE tipo_log;"));
来检查此问题。
要解决此问题,您需要将灯具类$canUseMemory
属性设置为false
。这将使您的灯具使用数据库引擎,您的表格是使用。
喜欢下一个:
class TipoLogFixture extends CakeTestFixture {
public $canUseMemory = false;
public $import = ['model' => 'TipoLog'];
public $records = [
...
];
}