Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Rspec 如何测试创建对象后发送的电子邮件_Rspec_Ruby On Rails 4 - Fatal编程技术网

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

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

  • 在调用@payment.save之前,移动您的
    预期值
  • 切换到
    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