我正在尝试使用 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
有任何想法吗?我不确定如何继续。
最佳答案
您是否在实际运行应用程序时测试过它是否有效?也许您的测试失败是正确的。
我注意到你从不给 deliver
打电话当您创建邮件时,所以我怀疑测试失败了,因为实际上没有发送电子邮件。我期待您的 send_welcome_email
看起来更像的方法
def send_welcome_email
AccountMailer.welcome(self).deliver
end
关于ruby-on-rails - ActionMailer::Base.deliveries 在 Rspec 中始终为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10952799/