ruby-on-rails – RSpec发送电子邮件(不存储在ActionMailer :: Base.deliveries中) – 不知道为什么?

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – RSpec发送电子邮件(不存储在ActionMailer :: Base.deliveries中) – 不知道为什么?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我使用rspec,当我运行rake规范时,用户邮件程序通过smtp发送电子邮件,而不是将电子邮件存储在ActionMailer :: Base.deliveries-array中(由用户观察者调用)…

你能给我一个提示吗?

# Rails version
rails -v
=> Rails 3.0.1

# Ruby version with rvm (rvm version 1.0.16)
ruby -v
=> ruby 1.9.2p7 (2010-09-29 revision 29373) [x86_64-darwin10.4.0]

# Gemfile
gem "rspec-rails",">= 2.0.1"

配置-文件

# config/environments/test.rb
MyApp::Application.configure do
  config.action_mailer.delivery_method = :test
  config.action_mailer.raise_delivery_errors = true
end

# spec/spec_helper.rb
...
ENV["RAILS_ENV"] ||= 'test'
...

# app/models/user_observer.rb
class UserObserver < ActiveRecord::Observer
  observe User

  def after_create(record)
    puts Rails.env
    # => "test"
    UserMailer.new_user_notification(record).deliver
    puts ActionMailer::Base.deliveries.inspect
    # => []
    # Sends it via smtp!
  end
end

解决方法

我太笨了……在我的setup_mail.rb-initializer中有“ActionMailer :: Base.delivery_method =:smtp”…… AAARGH ….

猜你在找的Ruby相关文章