0

我正在尝试测试电子邮件是否在对象的 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
4

1 回答 1

0

可能还有其他问题,但您在调用被测代码后设定了您的期望,因此在那之后没有“发生”任何事情。您需要:

  1. should在调用 @payment.save 之前移动您的期望
  2. 切换到expect { @payment.save }.to receive ...语法,或
  3. 使用“间谍”格式(即should_have_received
于 2013-09-27T15:19:44.720 回答