【发布时间】:2015-10-06 18:24:39
【问题描述】:
我正在尝试为一个脚本运行一个规范,该脚本将唯一化给定的表。
我现在在我的迁移中有独特的限制,所以我很难创建副本来测试我的脚本。
我目前的规格是这样的。
require 'spec_helper'
describe OneTime::UniquifyTypeDescriptorContext do
before(:each) do
duplicated_type_descriptor_contexts = FactoryGirl.build_list(:type_descriptor_context, 5, {:type_name => :foo})
ActiveRecord::Base.connection.execute('SET unique_checks=0;')
duplicated_type_descriptor_contexts.each{|tdc| tdc.save!(:validate => false)}
ActiveRecord::Base.connection.execute('SET unique_checks=1;')
FactoryGirl.create(:type_descriptor_context, :type_name => :bar)
end
context "#process!" do
it "should remove duplicates" do
OneTime::UniquifyTypeDescriptorContext.new.process!
expect(TypeDescriptorContext.count).to eq 2
end
end
end
但它一直在失败
ActiveRecord::RecordNotUnique:
ActiveRecord::JDBCError: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry 'foo' for key 'index_type_descriptor_contexts_on_type_name': INSERT INTO `type_descriptor_contexts` (`created_at`, `type_name`, `updated_at`) VALUES ('2015-10-06 18:23:39', 'foo', '2015-10-06 18:23:39')
我错过了什么吗?
【问题讨论】:
-
我不得不问,为什么您需要拥有/测试在无法创建重复记录的表中唯一化记录的功能?
-
因为以前不是这样的。我正在添加迁移以添加约束,但我需要先运行脚本以删除重复项。我的规范从运行所有迁移开始,因此它们运行添加约束的迁移,此时我无法创建错误数据来测试我的脚本。
标签: mysql ruby-on-rails rspec