我正在尝试测试电子邮件是否在对象的 after_create 回调中发送
class Payment < ActiveRecord::Base
#associations
belongs_to :quotation
#validations
validates :method, :amount, :quotation, :presence => true
#callbacks
after_create :send_email_confirmations
def method_enum
[['Cash'],['Bank Transfer'], ['PayPal']]
end
private
def send_email_confirmations
UsersMailer.confirm_payment(self).deliver
AdminsMailer.payment_received(self).deliver
end
end
测试文件:
require 'spec_helper'
describe Payment do
describe AdminsMailer do
before :each do
Admin.delete_all
User.delete_all
@quotation = FactoryGirl.create(:quotation)
@submission = @quotation.submission
@payment = Payment.new(:method => 'paypal', :amount => @quotation.price, :transaction_id => '4543332', :quotation_id => @quotation.id)
end
it "should deliver the admin email payment email" do
@payment.save
AdminsMailer.should_receive(:payment_received).with(@payment)
end
end
end
这会产生以下结果:
Failure/Error: AdminsMailer.should_receive(:payment_received).with(@payment)
(<AdminsMailer (class)>).payment_received(#<Payment id: 54, method: "paypal", amount: #<BigDecimal:7fabd1e449c0,'0.5E3',9(36)>, transaction_id: "4543332", created_at: "2013-09-27 11:30:53", updated_at: "2013-09-27 11:30:53", quotation_id: 58>)
expected: 1 time
如何测试电子邮件是否已发送?
received: 0 times