[rspec-users] Rspec, Mocha Expectations Not Met Bug?

David Chelimsky dchelimsky at gmail.com
Tue Dec 8 07:40:04 EST 2009

On Tue, Dec 8, 2009 at 2:17 AM, Sam Woodard <lists at ruby-forum.com> wrote:

> Code and tests at,
> http://gist.github.com/251480

The objects you're setting expectations on are not the same objects that are
being loaded by emailed_association_requests.to_send on line 4 of the gist.
They may have the same database IDs, but they don't have the same object_id

You've got two options here. One is to use mocha's any_instance on whatever
class emailed_association_request is written on.

The other, which I would recommend in this case, is to not use a mock. Mocks
are really best for cases where you have direct control over the
collaborator in a code example and deliver it directly to the object being
spec'd. In this case, the collaborator (ear) is retrieved through a side
door inside the contact import.

The other side benefit we can get from mocks and stubs is isolation from the
database, but these examples use real records, so that is obviously not a
concern in this case.

The first one can be spec'd by getting the id of ear before calling
ci.email, and then expecting RecordNotFound if you try to find using that

The 2nd one can be spec'd using a tool like email spec [1].

All of that said, there is a Feature Envy code smell [2] in the email
method. This method is asking the ear for data, acquiring other data using
that data, and then sending commands to the ear based on the state of that
user. I'd recommend simplifying this code per [3], moving the part of the
work that is based on the ear's data to the ear. If you do that, using a
mock in the spec for the email method makes a lot more sense (using
any_instance) because we're just concerned with a simple protocol and no
conditional logic.

The conditional logic would move to the AssociationRequest, and you can spec
the create_association_with method more simply because there are fewer
objects to set up.


[1] http://github.com/bmabey/email-spec
[2] http://c2.com/cgi-bin/wiki?FeatureEnvySmell
[3] http://gist.github.com/251599

I have debugged this code and both destroy and email! are being called!
> However, rspec/mocha says that the two expectations at the bottom of
> these tests are not met: both tests fail.
> Any help would be greatly appreciated.
> Thanks you in advance,
> Sam
> 1)
> Mocha::ExpectationError in 'ContactImport email should destroy the ear
> if the user is already a vbn member'
> not all expectations were satisfied
> unsatisfied expectations:
> - expected exactly once, not yet invoked:
> #<EmailedAssociationRequest:0x10685e088>.destroy(any_parameters)
> ...
> ./spec/models/contact_import_spec.rb:110:
> 2)
> Mocha::ExpectationError in 'ContactImport email should email the ear if
> the user is not already a vbn member'
> not all expectations were satisfied
> unsatisfied expectations:
> - expected exactly once, not yet invoked:
> #<EmailedAssociationRequest:0x1065fb700>.email!(any_parameters)
> ./spec/models/contact_import_spec.rb:120:
> Finished in 1.967623 seconds
> 16 examples, 2 failures
> --
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20091208/c7002ce9/attachment-0001.html>

More information about the rspec-users mailing list