Expectation failure that has to be a bug

1 view
Skip to first unread message

Sam

unread,
Dec 7, 2009, 11:41:24 PM12/7/09
to mocha-developer
Hi,

I've got two pretty simple tests, below, to test the following code:

def email
@emails = []
ContactImport.transaction do
emailed_association_requests.to_send.each do |ear|
if existing_user = User.find_by_email(ear.email)
ear.destroy
BusinessAssociation.new(:business =>
user.business, :other_business => existing_user.business).save!
else
ear.email!
end
end
end
end

The expectations at the end of each of the following tests fail, but
after debugging, the methods are clearly called and the ears in both
the test and in the code above have the same id. Totally stumped.
Been hacking on this for quite a while now.

Tests:


it 'should destroy the ear if the user is already a member' do
ci = Factory.create(:valid_contact_import)
ear = Factory.create(:valid_emailed_association_request)
user = Factory.create(:valid_user)
ci.user = user
ci.emailed_association_requests << ear
User.stubs(:find_by_email).returns(Factory.build(:valid_user))
ba = Factory.build(:valid_business_association)
ba.stubs(:save!)
BusinessAssociation.stubs(:new).returns(ba)

ear.expects(:destroy)
ci.email
end

it 'should email the ear if the user is not already a member' do
ci = Factory.create(:valid_contact_import)
ear = Factory.create(:valid_emailed_association_request)
ci.emailed_association_requests << ear
User.stubs(:find_by_email).returns(nil)

ear.expects(:email!)
ci.email
end

James Mead

unread,
Dec 8, 2009, 4:55:12 AM12/8/09
to mocha-d...@googlegroups.com
2009/12/8 Sam <sam.h....@gmail.com>:
I'm a bit short of time, but it looks to me like the "ears" are being
loaded from the database in the following line :-

emailed_association_requests.to_send.each do |ear|

This means that although the objects will have the same id (i.e.
database id), they will not have the same object_id i.e. they are not
the same *instances* as the ones in the tests.

One way to solve this would be to stub the
"emailed_association_requests" method to return the instances from the
tests.

I hope that helps.

Cheers, James.
Reply all
Reply to author
Forward
0 new messages