ruby-on-rails - ActionMailer::Base.deliveries 在 Rspec 中始终为空

标签 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

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

最佳答案

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

我注意到你从不给 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/

相关文章:

jquery - active_scaffold w/Rails 3.2 对搜索、编辑、新操作等没有给出可见的响应

ruby-on-rails - Rails 3 中的库应该放在哪里?

ruby-on-rails - Rails 有没有办法管理有序列表?这样每个项目都有一个位置,如果删除一个元素,位置会自动更新吗?

ruby-on-rails - 期望和expect_any_instance_of之间的区别

ruby-on-rails - 如何测试 ActiveRecord 中哪个验证失败?

ruby-on-rails - 运行 RSpec 测试时出现 NoMethodError

ruby-on-rails - Rails 4 - 为表单中关联表中的行分配值

ruby-on-rails - Rails 3.1、Ruby 1.9.2-p180 和 UTF-8 问题

具有有限子查询的 MySQL 查询

ruby-on-rails - 文件 Assets 指纹未在 Rails 3.1 上正确更新