I use Mocha, Test::Unit and delayed_job and wanted to test whether mailer method has been executed, like this:
test "should send info about paid payment" do
payment = Payment.new :contractor_name => "notempty", :address => "notempty"
UserMailer.expects(:send_payment_info)
payment.set_paid!
end
# model method
def set_paid!
self.status = 'paid'
self.paid_at = Date.today
self.save!
UserMailer.delay.send_payment_info self
end
Without using delay method test works as expected, but with it (and I have Delayed::Worker.delay_jobs = !Rails.env.test? in initializer config) I get:
NoMethodError: undefined method
deliver' for nil:NilClassset_paid!’
app/models/payment.rb:33:in
test/unit/payment_test.rb:50:in `test_should_send_info_about_paid_payment’
And I have no clue how to fix this? Maybe I should moch UserMailer differently?
Such workaround helped:
Don’t know if it’s the legit way to go, but it works