Ruby on rails 为什么我的Rspec测试会发送真正的邮件?

Ruby on rails 为什么我的Rspec测试会发送真正的邮件?,ruby-on-rails,testing,actionmailer,Ruby On Rails,Testing,Actionmailer,我教导说,在我的config/environments/test.rb中使用类似delivery\u method=:test的选项,在运行Rspec测试时,我不应该收到任何邮件: config/environments/test.rb: config.action_mailer.delivery_method = :test 但是在我的测试中,当我使用FactoryGirl创建一个用户,并且该用户在保存后有一个回调发送注册通知时,此电子邮件将被发送: myspec.rb: config.ac

我教导说,在我的
config/environments/test.rb中使用类似
delivery\u method=:test
的选项,在运行Rspec测试时,我不应该收到任何邮件:

config/environments/test.rb:

config.action_mailer.delivery_method = :test
但是在我的测试中,当我使用FactoryGirl创建一个用户,并且该用户在保存后有一个
回调发送注册通知时,此电子邮件将被发送:

myspec.rb:

config.action_mailer.delivery_method = :test
user=FactoryGirl.create(:user,:login=>'johndoe')
user\u observer.rb:

config.action_mailer.delivery_method = :test
class UserObserver
行动\u mailer.rb:

config.action_mailer.delivery_method = :test
ActionMailer::Base.delivery\u method=:smtp
ActionMailer::Base.smtp\u设置={
:地址=>“…”,
:端口=>“25”,
:domain=>“…”,
:user_name=>“…”,
:password=>“…”,
:身份验证=>:普通
}
有什么不对劲

我正在使用:

  • 轨道3.2.2
  • BuildinActionMailer
  • RSpec钢轨
  • 工厂女工
  • 卫兵

  • 是的,你是对的。ActionMailer::Base.delivery_method=:smtp在environment/test.rb中屏蔽您的配置

    我向您推荐下一个解决方案:为每个范围创建具有特定数据的夹具。
    像在
    就我而言,它看起来像:

    my config.yml

     development:
          support_mail: test@test.test
          smtp_user_name: test@test.test
          smtp_password: test
          smtp_domain: test.test
          smtp_address: test.test.test
          smtp_port: => 999
    
        test:
          support_mail: test@test.test
          smtp_user_name: test@test.test
          smtp_password: test
          smtp_domain: test.test
          smtp_address: test.test.test
          smtp_port: => 999
    
        production:
          support_mail: somth@somth.com
          smtp_user_name: somth@somth.com
          smtp_password: somth
          smtp_domain: somth.com
          smtp_address: smtp.somth.com
          smtp_port: => 587
    
    我的环境.rb

    # Load the rails application
    require File.expand_path('../application', __FILE__)
    #initialize custom config variables
    APP_CONFIG = YAML.load_file("#{Rails.root}/config/config.yml")[Rails.env]
        ActionMailer::Base.smtp_settings = {
          :user_name => APP_CONFIG["smtp_user_name"], #ENV['SENDGRID_USERNAME'],
          :password => APP_CONFIG["smtp_password"], #  ENV['SENDGRID_PASSWORD'],
          :domain => APP_CONFIG["smtp_domain"],
          :address => APP_CONFIG["smtp_address"],
          :port => APP_CONFIG["smtp_port"],
          :authentication => :plain,
          :enable_starttls_auto => false
        }
    ActionMailer::Base.delivery_method = :smtp
    

    这可能是因为,这个
    ActionMailer::Base.delivery\u方法=:smtp
    屏蔽了我在
    environment/test.rb
    中的配置?所以
    ActionMailer::Base.delivery\u方法就是罪魁祸首。我已经使用了特定于范围的配置(通过SettingsLogic)。