Activerecord 工厂女孩协会家长可以';不要空白
我正在尝试使用FactoryGirl创建一个测试数据库,该数据库具有一个关联,即父项有多个条目。此时,将抛出ActiveRecord验证错误,父级不能为空。我在这件事上经历了一段艰难的时光,我尝试了很多很多方法来创建这个测试数据库。我想我已经很接近了,但我甚至可能还没有接近,可能有一些基本的错误,所以任何和所有的建议都是非常感谢的 我猜散列{parent_id::id}没有被传递到条目工厂。这将使验证失败。但是,我不知道实际情况是这样的,即使是这样,我也不知道如何解决它。提前感谢你的帮助 错误是: ActiveRecord::RecordInvalid:验证失败:父级不能为空 RSpec呼叫是:Activerecord 工厂女孩协会家长可以';不要空白,activerecord,rspec,factory-bot,Activerecord,Rspec,Factory Bot,我正在尝试使用FactoryGirl创建一个测试数据库,该数据库具有一个关联,即父项有多个条目。此时,将抛出ActiveRecord验证错误,父级不能为空。我在这件事上经历了一段艰难的时光,我尝试了很多很多方法来创建这个测试数据库。我想我已经很接近了,但我甚至可能还没有接近,可能有一些基本的错误,所以任何和所有的建议都是非常感谢的 我猜散列{parent_id::id}没有被传递到条目工厂。这将使验证失败。但是,我不知道实际情况是这样的,即使是这样,我也不知道如何解决它。提前感谢你的帮助 错误是
before(:all) do
rand(11..25).times { FactoryGirl.create(:parent) }
visit "/parents?direction=asc&sort=parent_blog"
end
after(:all) do
Parent.delete_all
end
父模型是:
class Parent < ActiveRecord::Base
has_many :entries, dependent: :destroy
accepts_nested_attributes_for :entries, :allow_destroy => :true
validates :parent_blog, presence: true,
uniqueness: true
end
class Entry < ActiveRecord::Base
belongs_to :parent
validates :entry_blog, presence:true,
uniqueness: true
validates :parent_id, presence: true
end
进入工厂是:
FactoryGirl.define do
factory :parent do
sequence(:parent_blog) { |n| "http://www.parent%.3d.com/ss" % n }
entries { rand(5..15).times { FactoryGirl.create(:entry, parent_id: :id) } }
end
end
FactoryGirl.define do
factory :entry do
sequence(:entry_blog) { |n| "http://www.parent%.3d.com/ss/entry%.3d" % [n, n] }
parent_id { :parent_id }
end
end
以下对工厂定义的修改应该有效。我稍后会回来解释
FactoryGirl.define do
factory :parent do
sequence(:parent_blog) { |n| "http://www.parent%.3d.com/ss" % n }
after(:create) {|parent| rand(5..15).times {FactoryGirl.create(:entry, parent: parent)}}
end
factory :entry do
sequence(:entry_blog) { |n| "http://www.parent%.3d.com/ss/entry%.3d" % [n, n] }
parent
end
end
如中所述,在:parent
工厂中引入之后使用,以及使用parent
代替parent\u id
,这两项更改都是RSpec支持关联的示例
至于为什么你的代码不起作用,我现在只能提供部分答案。不能将条目
作为初始父项创建的一部分的原因是,在创建父项记录之前,您没有父项id,但是您需要父项id来创建条目,因为条目
验证了父项id
的存在。换句话说,在父工厂评估条目
块时,父\u id
尚未设置
我不确定的是,为什么您不能在entry factory中用parent\u id
替换parent
,并在FactoryGirl中用parent\u id:parent.id
相应地替换parent:parent
。在父工厂中创建调用。在提交上述内容之前,我尝试了该变体,但失败的原因是:
1) test
Failure/Error: rand(11..25).times { FactoryGirl.create(:parent) }
ArgumentError:
Trait not registered: parent_id
# ./spec/factories.rb:4:in `block (4 levels) in <top (required)>'
# ./spec/factories.rb:4:in `times'
# ./spec/factories.rb:4:in `block (3 levels) in <top (required)>'
# ./tmp/factory_spec.rb:5:in `block (3 levels) in <top (required)>'
# ./tmp/factory_spec.rb:5:in `times'
# ./tmp/factory_spec.rb:5:in `block (2 levels) in <top (required)>'
1)测试
失败/错误:rand(11..25).times{FactoryGirl.create(:parent)}
参数错误:
未注册特征:父\u id
#./spec/factories.rb:4:in'block(4层)in'
#./spec/factories.rb:4:in'times'
#./spec/factories.rb:4:in'block(3层)in'
#/tmp/factory_规范rb:5:in‘分块(3级)in’
#/tmp/工厂规格rb:5:以“次”为单位
#/tmp/factory_规范rb:5:in‘分块(2级)in’
如果我弄明白了,我会再次更新这个答案。我当然会喜欢听这个解释,因为我不明白。after(:create)很有意义,事实上,我已经在一次迭代中尝试过了。然而,在factory:entry中对“父级”的引用是我不理解的。我认为它提供/支持协会。不管怎样,你的建议效果很好。谢谢。很抱歉这么久才给你回复。请参阅更新的答案。这一切都对我有效,无需进一步跟进。谢谢