Bvu*_*Ic7 10 email rspec ruby-on-rails observer-pattern ruby-on-rails-3
我使用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"
Run Code Online (Sandbox Code Playgroud)
配置-文件:
# 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
Run Code Online (Sandbox Code Playgroud)
Bvu*_*Ic7 15
我太笨了......在我的setup_mail.rb-initializer中有"ActionMailer :: Base.delivery_method =:smtp"...... AAARGH ....
详细说明这个问题的解决方案:
在您的config/enviroments/test.rb中,默认情况下您应该拥有该行 config.action_mailer.delivery_method = :test
这样做是告诉ActionMailer不要正常发送电子邮件,而是将发送的电子邮件存储在数组中ActionMailer::Base.deliveries.这对测试很有用,因为您可以通过在ActionMailer :: Base.deliveries数组上使用inspect,count,length方法来判断已经发送了多少封电子邮件.
但是,如果您将交付方式设置为类似config.action_mailer.delivery_method = :smtp,那可能会覆盖您之前的delivery_method =:test; 因此,您的ActionMailer::Base.deliveries人口不会被填充.
我在使用MailCatcher查看正在发送的电子邮件时完成了这一操作,从而导致我的测试失败,即使我确定电子邮件已正确发送!
因此,请确保您没有在测试环境中设置除test:test之外的delivery_method.
作为旁注:如果您正在使用Devise,则可能需要检查Devise.mailer.deliveries阵列.
| 归档时间: |
|
| 查看次数: |
3284 次 |
| 最近记录: |