我使用Mocha,Test :: Unit和delayed_job并想测试是否已执行邮件程序方法,如下所示:
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
不使用延迟方法测试按预期工作,但有了它(我在初始化器配置中有Delayed :: Worker.delay_jobs =!Rails.env.test?)我得到:
NoMethodError:未定义的方法
deliver' for nil:NilClass app/models/payment.rb:33:in
set_paid!' test / unit / payment_test.rb:50:in,test_should_send_info_about_paid_payment'
我不知道如何解决这个问题?也许我应该以不同的方式使用UserMailer?
答案 0 :(得分:5)
这种解决方法有所帮助:
UserMailer.stubs(:delay).returns UserMailer
UserMailer.expects(:send_payment_info)
不知道这是否合法,但它有效