ActionMailer :: Base.deliveries在Rspec中始终为空

时间:2012-06-08 16:38:16

标签: ruby-on-rails ruby-on-rails-3 rspec rspec2 rspec-rails

我试图用rspec测试一些邮件程序但是交付总是空的。这是我的rspec测试:

require "spec_helper"

describe "AccountMailer", :type => :helper do
  before(:each) do
    ActionMailer::Base.delivery_method = :test
    ActionMailer::Base.perform_deliveries = true
    ActionMailer::Base.deliveries = []
  end

  it "should send welcome email to account email" do
    account = FactoryGirl.create :account
    account.send_welcome_email

    ActionMailer::Base.deliveries.empty?.should be_false
    ActionMailer::Base.deliveries.last.to.should == account.email
  end
end

失败了:

1) AccountMailer should send welcome email to account email
     Failure/Error: ActionMailer::Base.deliveries.empty?.should be_false
        expected true to be false

我的send_welcome_email函数看起来像这样(那是我的模特):

def send_welcome_email 
    AccountMailer.welcome self
end

我的邮件:

class AccountMailer < ActionMailer::Base
  default from: APP_CONFIG['email']['from']

  def welcome data
    if data.locale == 'es'
      template = 'welcome-es'
    else
      template = 'welcome-en'
    end

    mail(:to => data.email, :subject => I18n.t('welcome_email_subject'), :template_name => template)
  end
end

有什么想法吗?我不确定如何继续。

1 个答案:

答案 0 :(得分:14)

您是否测试过它在您实际运行应用程序时有效?也许你的测试是失败的。

我注意到你在创建邮件时从不打电话给deliver,所以我怀疑测试失败了,因为电子邮件实际上并没有被发送。我希望您的send_welcome_email方法看起来更像

def send_welcome_email 
  AccountMailer.welcome(self).deliver
end