Rspec 如何测试创建对象后发送的电子邮件
我试图测试电子邮件是否在对象的创建回调后发送Rspec 如何测试创建对象后发送的电子邮件,rspec,ruby-on-rails-4,Rspec,Ruby On Rails 4,我试图测试电子邮件是否在对象的创建回调后发送 class Payment < ActiveRecord::Base #associations belongs_to :quotation #validations validates :method, :amount, :quotation, :presence => true #callbacks after_create :send_email_confirmations
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
这将产生以下结果:
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
可能还有其他问题,但您在调用测试中的代码后设置了期望值,因此在这之后没有“发生”任何事情。您需要:
预期值
expect{@payment.save}。接收…
语法,或应已收到
)我听从了你的建议。感谢您将我发送到正确的路径。我使用了Object.any_实例。是否应接收(:payment_received)和then@payment=Payment.save您是否有理由不将期望设置限制为期望接收该方法的实际对象?
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