Rails测试 – has_many关联的夹具
发布时间:2020-12-17 02:06:42 所属栏目:百科 来源:网络整理
导读:我是单元测试的新手,有一个简单的用例. 有2个型号:城市,居民.一个城市有很多居民. 我创建了2个fixture yml文件:cities.yml,resident.yml. residents.yml resident1: name: resident1resident2: name: resident2resident3: name: resident3 cities.yml city
我是单元测试的新手,有一个简单的用例.
有2个型号:城市,居民.一个城市有很多居民. 我创建了2个fixture yml文件:cities.yml,resident.yml. residents.yml resident1: name: resident1 resident2: name: resident2 resident3: name: resident3 cities.yml city1: name: city1 residents: resident1,resident2,resident3 当我运行一个应该总是通过的琐碎测试时,我收到一个错误: Minitest::UnexpectedError: ActiveRecord::StatementInvalid: Mysql2::Error: Unknown column 'residents' in 'field list': INSERT INTO `cities` (`name`,`residents`,`created_at`,`updated_at`,`id`) VALUES ('city1','resident1,resident3','2014-06-09 20:42:22',574963714) 我期望的是具有属性名称的City模型实例:’city1′,以及属于3个Resident模型实例的数组的属性. 在City.rb中,我指定了与Resident模型的has_many关系.在Resident.rb里面,我指定了与City模型的belongs_to关系. 这应该是一件简单的事情,不应该吗? 更新#1: 似乎只能通过设置Resident灯具的城市属性来实现. residents.yml resident1: name: resident1 city: city1 # added resident2: name: resident2 city: city1 # added resident3: name: resident3 city: city1 # added cities.yml city1: name: city1 # residents: resident1,resident3 我想它必须足够了. 解决方法
我认为UPDATE#1没问题.毕竟,关联实际上是如何存储在数据库中的,而固定装置只是种子数据库的一种方式.
你可以使用FactoryGirl或你自己的代码来干掉它,如果你真的想要,但是测试应该是DAMP not DRY并且扩展到灯具. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |