CakePHP测试:尽管有外键约束,数据仍然成功保存

时间:2015-01-17 04:19:02

标签: mysql unit-testing cakephp

我正在对我的应用程序控制器进行单元测试,该控制器可以保存来自REST调用的数据。我使用外键vehicle_id = 99传递数据,这在Fixture数据中不存在,但在测试中它已成功保存!这不是我想要的 - 我希望保存失败。

这是我的夹具和型号:

class RouteFixture extends CakeTestFixture {
    public $import = array('model' => 'Route');
}

class Route extends AppModel {

    public $belongsTo = array(
        'Vehicle' => array(
            'className' => 'Vehicle',
            'foreignKey' => 'vehicle_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );

SQL

CREATE TABLE `routes` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `account_id` int(11) NOT NULL,
 `created` datetime NOT NULL,
 `vehicle_id` int(11) NOT NULL,
 PRIMARY KEY (`id`),
 KEY `account_id` (`account_id`),
 KEY `vehicle_id` (`vehicle_id`),
 CONSTRAINT `routes_ibfk_2` FOREIGN KEY (`vehicle_id`) REFERENCES `vehicles` (`id`),
 CONSTRAINT `routes_ibfk_1` FOREIGN KEY (`account_id`) REFERENCES `accounts` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1

以下是我在测试中保存后在数据库中找到的内容

    (int) 1 => array(
            'Route' => array(
                    'id' => '2',
                    'account_id' => '0',
                    'created' => '2015-01-17 05:05:51',
                    'vehicle_id' => '99'
            ),
            'Account' => array(
                    'id' => null
            ),
            'Vehicle' => array(
                    'id' => null,
                    'name' => null,
                    'account_id' => null
            ),
            'Address' => array()
    )

请告知为什么在测试中未检查约束。谢谢

0 个答案:

没有答案