我正在使用ActionMailer 4.0.0开发Ruby on Rails 4.0.0。尽管遵循Rails指南,我仍然无法发送电子邮件,也无法成功测试。从rails控制台检查ActionMailer实例我正在观察正在实例化的NullMailer,它(什么都不做)TM
>> rails c
Loading development environment (Rails 4.0.0)
2.0.0p247 :001 > er = EventRegistration.last
2.0.0p247 :003 > MainMailer.event_registration(er)
=> #<ActionMailer::Base::NullMail:0x007fc11cbe3108>
我对Rails指南以及ActionMailer代码库中的注释进行了双重检查。这是我的文件:
的Gemfile:
source 'https://rubygems.org'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '= 4.0.0'
# Use SCSS for stylesheets
gem 'sass-rails', '= 4.0.0'
# Use HAML for HTML
gem 'haml-rails'
# Use CoffeeScript for .js.coffee assets and views
gem 'coffee-rails'
gem 'jquery-rails'
gem 'jquery-ui-rails'
gem 'el_finder'
gem 'uglifier'
gem 'therubyracer'
gem 'turbolinks'
gem 'kaminari', '~> 0.14.1'
gem 'devise', '~> 3.0.0'
group :doc do
gem 'sdoc', require: false
end
group :development do
gem 'thin'
gem 'erb2haml'
gem 'capistrano'
gem 'rvm-capistrano'
gem 'rspec-rails'
end
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder'
gem 'mysql2', '~> 0.3.12b5'
gem 'thinking-sphinx'
gem 'pdf-writer', github: "mwlang/pdf-writer"
gem 'pothoven-attachment_fu', github: 'mwlang/attachment_fu'
gem 'prototype-rails'
gem 'acts_as_list'
gem 'rails-observers'
gem 'dynamic_form'
gem 'formtastic', github: 'justinfrench/formtastic'
gem 'activeadmin', github: 'mwlang/active_admin', branch: 'rails4'
gem 'ransack', github: 'ernie/ransack', branch: 'rails-4'
gem 'inherited_resources', github: 'josevalim/inherited_resources'
gem "will_paginate"
gem "whenever"
gem 'vcard'
gem 'icalendar'
gem 'builder'
gem 'rmagick'
gem 'activeadmin-sortable-tree', github: 'nebirhos/activeadmin-sortable-tree'
gem 'acts_as_tree'
gem "html_helpers", github: 'mwlang/html_helpers'
gem 'better_errors', group: :development
gem 'quiet_assets', group: [:development, :test]
gem "guard-rails", group: :development
gem 'guard-rspec', require: false, group: :development
在config/environments/test.rb
我有:
# 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
我的EventRegistration
型号:
class EventRegistration < ActiveRecord::Base
validates_presence_of :first_name, :last_name, :email, :event_id
validates_format_of :email,
:with => /\A[^\s@]+@[^\s@]+\.[^\s@]+\z/,
:message => "was not well-formed"
belongs_to :event
def send_notifications
EventMailer.event_registration(self).deliver
EventMailer.event_confirmation(self).deliver
end
end
EventRegistration
的规范:
require 'spec_helper'
describe EventRegistration do
let(:event) {
NormalEvent.create({
"name"=>"Whats up Doc?",
"type_of_event"=>"Cartoon",
"presented_by"=>"Looney Tunes",
"speakers"=>"Donald Duck",
"duration"=>"30 minutes",
"status_id"=>"100",
"allow_registration"=>"1",
"starts_on(1i)"=>"2015",
"starts_on(2i)"=>"3",
"starts_on(3i)"=>"11",
"start_time"=>"06:35",
"end_time"=>"07:00",
"hide_date"=>"0",
"summary"=>"Not much",
"details"=>"<p>Cartoon with the wabbit in it.</p>"
})
}
let(:registration) {
EventRegistration.create({
"first_name"=>"Bugs",
"title"=>"Wabbit",
"address1"=>"123 Nowhere St",
"city"=>"Anyville",
"postal_code"=>"12345",
"email"=>"bugs.bunny@looneytunes.com",
"last_name"=>"Bunny",
"company_name"=>"Looney Tunes",
"state"=>"GA",
"country"=>"USA",
"phone"=>"555-555-1234",
"event_id" => event.id}
})
}
it "should initialize" do
assert_equal true, event.valid?
assert_equal true, registration.valid?
end
it "should send notifications" do
registration.send_notifications
assert !ActionMailer::Base.deliveries.empty?
end
end
assert !ActionMailer::Base.deliveries.empty?
行失败
为什么NullMailer会被实例化而不是TestMailer的任何想法?为了它的价值,即使已经配置了SMTP设置,NullMailer也会被实例化用于生产。