Topic: Factory Girl: overriding a foreign key / association
If I have a factory that assembles the associations of a record, I can't figure out how to call the factory to override the foreign key - it gets set to the association from the factory file no matter what I pass to Factory.create(). I want to do this so I can spec out the behavior of the record validations when the foreign key is nil, or when it points to a non-existent record.
Suppose I have these two classes and their factories:
class Degree < ActiveRecord::Base
belongs_to :institution, :foreign_key => "institution_code"
# ... other associations ...
def validate
if institution_code.nil? or institution.nil?
errors.add(:institution_code, "Must be a valid institution")
end
end
end
class Institution < ActiveRecord::Base
end
Factory.define :degree do |d|
d.association :institution, :factory =>:institution
# other attributes and associations here
end
Factory.define :institution do |i|
i.institution_code "00001"
i.name "International Institute of Rails"
endThe problem with this approach is that even if I call:
Factory(:degree, :institution_code => nil) or
Factory(:degree, :institution_code => '00002')
I get back a Degree object that has institution_code set to "00001".
I could of course create a specifically defined :degree factory that didn't set up associations, so I could just set the foreign keys as attributes. But I actually have 5 associations to test, and each needs to fail if it's set to nil or to point to a non-existent record. So that would mean for each nil association I wanted to test, I'd have to manually set the other 4 to valid values; that gets tedious in a hurry and defeats the purpose of using factories.
Thoughtbot docs don't explain how to do this, and Google has been unhelpful on this issue. Any advice appreciated, thanks.
Last edited by IdahoEv (2009-09-23 22:55:42)