[rspec-users] a MockExpectationError problem

Patrick J. Collins
Sun, 25 Apr 2010 08:43:35 -0000

Hi everyone,

I have a test for importing vcards, and I was finding that each time it was
being run, it was generating new photos and since it was using the test
database, it was actually overwriting real photos that were being stored in the
file system (they are named based on the record''s id).  So it was
apparent to
me that I need to use mocking to intercept Photo.create so that it
doesn''t
really happen...

I tried to do this, but I am getting this MockExpectationError, which I
don''t quite understand the meaning of :[]
---

The relationships are:

Photo belongs_to :contact
Contact has_one :photo

---

this is how I am mocking in my test:

@photo = mock_model(Photo)
Photo.should_receive(:create).and_return(@photo)


---

the relevant code that is getting called to create the photo:

def card_photo
  @contact.photo = Photo.create(:image_file_string => @card.photos.first)
end

---

And... the error:
1)
Spec::Mocks::MockExpectationError in ''Vcard importing a vcard a card
with an invalid address, a photo, birthday, url, phone, and email should update
an existing contact when an email is found''
Mock "Photo_1001" received unexpected message :[]= with
("contact_id", 201)
/Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/associations/association_proxy.rb:185:in
`set_belongs_to_association_for''
/Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/associations/has_one_association.rb:56:in
`replace''
/Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/associations.rb:1281:in
`photo=''
/Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/associations/association_proxy.rb:217:in
`send''
/Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/associations/association_proxy.rb:217:in
`method_missing''
/Users/patrick/coding/rails/xyz/app/models/vcard.rb:164:in `card_photo''

----


Any ideas?

Thanks..