On Tue, Dec 8, 2009 at 2:17 AM, Sam W. [email protected]
wrote:
Code and tests at,
gist:251480 · GitHub
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
values.
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
id.
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.
HTH,
David
[1] GitHub - email-spec/email-spec: Collection of RSpec/MiniTest matchers and Cucumber steps for testing email in a ruby app using ActionMailer or Pony
[2] http://c2.com/cgi-bin/wiki?FeatureEnvySmell
[3] gist:251599 · GitHub
I have debugged this code and both destroy and email! are being called!