我正在尝试测试电子邮件是在对象的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
答案 0 :(得分:0)
可能还有其他问题,但您在调用测试代码后设置了您的期望,因此在此之后没有“发生”。你需要:
should
期望值
expect { @payment.save }.to receive ...
语法或should_have_received
)