Make FakeServiceUser assoc methods tolerant of nil
This commit is contained in:
parent
934a91e47e
commit
f9ef9a4b47
2 changed files with 10 additions and 1 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in a new issue