Ruby on rails 3.1 ActionMailer::Base.deliveries具有N次最后一封电子邮件

Ruby on rails 3.1 ActionMailer::Base.deliveries具有N次最后一封电子邮件,ruby-on-rails-3.1,actionmailer,Ruby On Rails 3.1,Actionmailer,我有一个ActionMailer,它有一个接受数组的方法,它为数组中的每个项目发送一封电子邮件。我想在测试中检查ActionMailer::Base.deliveries,看看是否发送了正确的电子邮件。首先,我检查发送的电子邮件数量: ActionMailer::Base.deliveries.size.should be == my_array.size 是的,断言按预期传递 但是ActionMailer::Base.deliveries似乎已经发送了最后一封电子邮件的所有克隆。手动测试让我

我有一个ActionMailer,它有一个接受数组的方法,它为数组中的每个项目发送一封电子邮件。我想在测试中检查
ActionMailer::Base.deliveries
,看看是否发送了正确的电子邮件。首先,我检查发送的电子邮件数量:

ActionMailer::Base.deliveries.size.should be == my_array.size
是的,断言按预期传递


但是
ActionMailer::Base.deliveries
似乎已经发送了最后一封电子邮件的所有克隆。手动测试让我确信,
ActionMailer::Base.deliveries
在欺骗我——事实上,唯一的电子邮件是被发送的,而不是最后一封N次。我不明白吗?

我也遇到了这种奇怪的行为(Rails 3.2.9),最终使用了这种丑陋的解决方法:

  email = mail(:from => options[:from], :to => options[:to], :subject => options[:subject]) do |format|
    format.text { render :text => options[:text] }
  end
  if Rails.env.test?
    email = Marshal.load( Marshal.dump(email) )
  end
  email.deliver

请注意,.dup或.clone不起作用-.to和.from属性将仍然相同。

我也遇到了这种奇怪的行为(Rails 3.2.9),并最终使用了这种丑陋的解决方法:

  email = mail(:from => options[:from], :to => options[:to], :subject => options[:subject]) do |format|
    format.text { render :text => options[:text] }
  end
  if Rails.env.test?
    email = Marshal.load( Marshal.dump(email) )
  end
  email.deliver

请注意,.dup或.clone不起作用-.to和.from属性仍然相同。

我在Rails 4中也有同样的问题。我在我的模型中发送了两封邮件,它们确实不同,但在测试中,我在交付阵列中收到了两封相同的邮件:
mail(收件人:@owner.email,主题:t('booking\u cancelled.action'),模板名称:“booking\u cancelled\u to\u owner”)。交付!邮件(发送至:@booker.email,主题:t('booking\u cancelled.action'),模板名称:“booking\u cancelled\u to\u booker”)。发送我在Rails 4中也有同样的问题。我在我的模型中发送了两封邮件,它们确实不同,但在测试中,我在交付阵列中收到了两封相同的邮件:
mail(收件人:@owner.email,主题:t('booking\u cancelled.action'),模板名称:“booking\u cancelled\u to\u owner”)。交付!邮件(发送至:@booker.email,主题:t('booking\u cancelled.action'),模板名称:“booking\u cancelled\u to\u booker”)。发送