Let me remind you of the difference: instance_double
can fail a test if methods are not available in the specified class, double
doesn't care about anything.
In my experience double
is needed in two cases:
1. Instead of an object that doesn't yet exist in the system. There is no class, so instance_double
has nothing to rely on.
2. Instead of something minor with a stable API. For example, for emails:
allow(DeadlineMailer)
.to receive(:last_deadline_warning)
.and_return(double(:email, deliver_later: true))
Top comments (2)
Could you explain a little more about the return here?
Later in the test, are you testing that the email has been delivered?
I believe that using a strict return double i.g. instance_double allows you to be more explicit with your return.
Not in this case. If I'd have to test that the email has been delivered, I'd assign it and verify:
In this particular case I simply stub emails in order to speed up tests. Consider the following example:
Without stub we'd have to render and send deadline email 4 times which could be expensive and time consuming.