首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >ActionMailer:测试失败,但可在开发环境中工作

ActionMailer:测试失败,但可在开发环境中工作
EN

Stack Overflow用户
提问于 2011-12-01 18:39:06
回答 4查看 3.1K关注 0票数 3

我的Rails3.1项目中的ActionMailer有一个奇怪的行为,ActionMailer::Base.deliveries在测试中是空的,而我实际上可以通过在Rails控制台中运行代码来接收电子邮件。有谁能指出这是错的吗?

代码语言:javascript
运行
复制
# spec/mailers/user_mailer_spec.rb
require "spec_helper"

describe UserMailer do
  describe ".reminding_email" do
    subject { UserMailer.reminding_email(user).deliver }
    let(:user) { create :confirmed_user_with_mass_tasks }
    it "should be delivered" do
      ActionMailer::Base.deliveries.should_not be_empty
    end
    its(:to) { should == [user.email] }
    its(:subject) { should == "Task Reminding" }
  end
end

# app/mailers/user_mailer.rb
class UserMailer < ActionMailer::Base
  layout "email"
  default from: Rails.env.production? ? "<production>@gmail.com" : "<test>@gmail.com"
  def reminding_email(user)
    @tasks = user.tasks.reminding_required
    @current = Time.current
    mail(to: user.email, subject: "Task Reminding")
  end
end

失败:

1) UserMailer.reminding_email应交付失败/错误: ActionMailer::Base.deliveries.should_not be_empty预期为空?要返回false,请获取true # ./spec/mailers/user_mailer_spec.rb:8:in‘`block (3 Level) in’

在11.81秒内完成42个示例,1个失败,2个待定

代码语言:javascript
运行
复制
# config/environments/test.rb
RemindersForMe::Application.configure do
  # Settings specified here will take precedence over those in config/application.rb

  # The test environment is used exclusively to run your application's
  # test suite.  You never need to work with it otherwise.  Remember that
  # your test database is "scratch space" for the test suite and is wiped
  # and recreated between test runs.  Don't rely on the data there!
  config.cache_classes = true

  # Configure static asset server for tests with Cache-Control for performance
  config.serve_static_assets = true
  config.static_cache_control = "public, max-age=3600"

  # Log error messages when you accidentally call methods on nil
  config.whiny_nils = true

  # Show full error reports and disable caching
  config.consider_all_requests_local       = true
  config.action_controller.perform_caching = false

  # Raise exceptions instead of rendering exception templates
  config.action_dispatch.show_exceptions = false

  # Disable request forgery protection in test environment
  config.action_controller.allow_forgery_protection    = false

  # Tell Action Mailer not to deliver emails to the real world.
  # The :test delivery method accumulates sent emails in the
  # ActionMailer::Base.deliveries array.
  config.action_mailer.delivery_method = :test

  # Use SQL instead of Active Record's schema dumper when creating the test database.
  # This is necessary if your schema can't be completely dumped by the schema dumper,
  # like if you have constraints or database-specific column types
  # config.active_record.schema_format = :sql

  # Print deprecation notices to the stderr
  config.active_support.deprecation = :stderr

  # Setup default URL options as devise needed
  config.action_mailer.default_url_options = { :host => 'localhost:3000' }
end

应用程序:

代码语言:javascript
运行
复制
# config/application.rb
require File.expand_path('../boot', __FILE__)

# Pick the frameworks you want:
require "active_record/railtie"
require "action_controller/railtie"
require "action_mailer/railtie"
require "active_resource/railtie"
require "sprockets/railtie"
# require "rails/test_unit/railtie"

if defined?(Bundler)
  # If you precompile assets before deploying to production, use this line
  # Bundler.require(*Rails.groups(:assets => %w(development test)))
  Bundler.require *Rails.groups(:assets) if defined?(Bundler)
  # If you want your assets lazily compiled in production, use this line
  # Bundler.require(:default, :assets, Rails.env)
end

module RemindersForMe
  class Application < Rails::Application
    # Settings in config/environments/* take precedence over those specified here.
    # Application configuration should go into files in config/initializers
    # -- all .rb files in that directory are automatically loaded.

    # Custom directories with classes and modules you want to be autoloadable.
    # config.autoload_paths += %W(#{config.root}/extras)

    # Only load the plugins named here, in the order given (default is alphabetical).
    # :all can be used as a placeholder for all plugins not explicitly named.
    # config.plugins = [ :exception_notification, :ssl_requirement, :all ]

    # Activate observers that should always be running.
    # config.active_record.observers = :cacher, :garbage_collector, :forum_observer

    # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
    # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
    # config.time_zone = 'Central Time (US & Canada)'

    # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
    # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
    # config.i18n.default_locale = :de

    # Configure the default encoding used in templates for Ruby 1.9.
    config.encoding = "utf-8"

    # Configure sensitive parameters which will be filtered from the log file.
    config.filter_parameters += [:password]

    # Enable the asset pipeline
    config.assets.enabled = true

    # Version of your assets, change this if you want to expire all your assets
    config.assets.version = '1.0'

    # Set Rspec generator as default
    config.generators do |g|
      g.test_framework :rspec
    end

    # Set Mailer of Devise
    config.to_prepare do
      Devise::Mailer.layout "email"
    end
  end
end

现在更改规范,如下所示:

代码语言:javascript
运行
复制
require "spec_helper"

describe UserMailer do
  describe ".reminding_email" do
    subject { UserMailer.reminding_email(user) }
    let(:user) { create :confirmed_user_with_undue_tasks }
    it { expect { subject.deliver }.to change { ActionMailer::Base.deliveries.length }.by(1) }
    its(:to) { should == [user.email] }
    its(:subject) { should == "Task Reminding" }
  end
end

1)邮件失败/错误: it { expect { subject.deliver }.to change { ActionMailer::Base.deliveries.length }.by( 1 ) }结果本应由1更改,但已由2#更改。/spec/mailers/user_mailer_spec.rb:7: in ` `block (3 Level)in‘

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2011-12-01 22:30:43

您应该测试邮件的分离属性:

代码语言:javascript
运行
复制
describe UserMailer do
  describe ".reminding_email" do
    subject { UserMailer.reminding_email(user)}
    let(:user) { create :confirmed_user_with_mass_tasks }
    it{ expect{subject.deliver}.to change{ActionMailer::Base.deliveries.length}.by(1)}
    its(:to) { should == [user.email] }
    its(:subject) { should == "Task Reminding" }
  end
end

注意主题中的变化,因为您的邮件发送程序应该返回已经用tosubject初始化的mail方法结果。

票数 1
EN

Stack Overflow用户

发布于 2012-01-06 08:04:03

我遇到了类似的问题,因为我正在测试Devise电子邮件: Devise使用一个单独的邮件程序,所以你需要访问Devise.mailer.deliveries而不是ActionMailer::Base.deliveries

票数 8
EN

Stack Overflow用户

发布于 2013-03-22 15:04:37

默认情况下,Action Mailer不会在测试环境中发送电子邮件。它们只是被添加到ActionMailer::Base.deliveries数组中。(参见http://guides.rubyonrails.org/action_mailer_basics.html中的第6节)

“默认情况下”的意思是您不应该为测试环境提供有效的email_delivery设置。如果“email_delivery /Configuration.yml”中有有效的默认配置设置,您可以使用以下命令覆盖它:

代码语言:javascript
运行
复制
test:
  email_delivery:
    delivery_method: :test
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8339957

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档