Make FakeServiceUser assoc methods tolerant of nil

This commit is contained in:
Raphael Sofaer 2011-05-17 10:14:24 -07:00
parent 934a91e47e
commit f9ef9a4b47
2 changed files with 10 additions and 1 deletions

View file

@ -44,7 +44,11 @@ class FakeServiceUser < HashWithIndifferentAccess
ServiceUser.reflect_on_all_associations.each do |assoc|
define_method assoc.name do
assoc.klass.find(self[assoc.primary_key_name])
if associated_id = self[assoc.primary_key_name]
assoc.klass.find(associated_id)
else
nil
end
end
end
end

View file

@ -137,5 +137,10 @@ describe FakeServiceUser do
Person.should_receive(:find).with(@data[5]).and_return person
@fake.person.should == person
end
it 'does not error on an association with no id' do
@fake[:person_id] = nil
lambda{ @fake.person }.should_not raise_error
end
end
end