我需要一个至少有两个条目的 has_many 关联,我如何编写验证以及如何使用 RSpec + factory-girl 对其进行测试?这是我到目前为止所得到的,但它失败了ActiveRecord::RecordInvalid: Validation failed: Bars can't be blank
,我完全陷入了 RSpec 测试。
/example_app/app/models/foo.rb
class Foo < ActiveRecord::Base
has_many :bars
validates :bars, :presence => true, :length => { :minimum => 2}
end
/example_app/app/models/bar.rb
class Bar < ActiveRecord::Base
belongs_to :foo
validates :bar, :presence => true
end
/example-app/spec/factories/foo.rb
FactoryGirl.define do
factory :foo do
after(:create) do |foo|
FactoryGirl.create_list(:bar, 2, foo: foo)
end
end
end
/example-app/spec/factories/bar.rb
FactoryGirl.define do
factory :bar do
foo
end
end