1
votes

Here's the call I would like to test expectations for:

UserMailer.invoice_paid(user, invoice).deliver_later

Rails 5 ActionMailer behaviour in :test mode seems to return nil on every method of an ApplicationMailer class. This should be fine since I can just stub it like so:

invoice_paid_dbl = double(ActionMailer::MessageDelivery)
allow(UserMailer).to receive(:invoice_paid).and_return(invoice_paid_dbl)

This expectation fails:

expect(UserMailer).to receive(:invoice_paid).once

While this one passes:

expect(invoice_paid_dbl).to receive(:deliver_later).once

Shouldn't that be impossible? I thought maybe the method chain was confusing RSpec, but splitting up the line into this has no effect:

mail = UserMailer.invoice_paid(user, invoice)
mail.deliver_later

Adding .with(any_args) to the stub and expectation also has no effect (since that's the default anyways). The failure is expected: 1 time with any arguments, received: 0 times with any arguments.

2
how you have stubbed the method?Ganesh

2 Answers

0
votes

Not getting how you have stubbed methods, but think that following will work for you

You are trying to stub chain of methods so need to use stub_chain instead of stub

UserMailer.stub_chain(:invoice_paid, :deliver_later).and_return(:invoice_paid_dbl)

I hope this will work and solve you problem. For more details please check docs

0
votes

I'll leave the question up in case it helps others, but this was actually the result of a silly mistake. I had expect(UserMailer).to receive(:invoice_paid) earlier in the it block, and it was passing. It was the second expectation of that type which was failing. Oops.