質問

I'm working on Ruby on Rails 4.0.0 with ActionMailer 4.0.0. I am unable to send emails despite following the Rails guides, nor am I able to successfully test. Inspecting the ActionMailer instance from rails console I am observing the NullMailer being instantiated, which (does nothing)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> 

I've double-checked everything against the Rails guides and even the comments within the ActionMailer's code base. Here are my files:

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

In config/environments/test.rb I have:

# 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

My EventRegistration model:

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

And the spec for 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

The assert !ActionMailer::Base.deliveries.empty? line fails

Any ideas why a NullMailer would be instantiated instead of the TestMailer? For what its worth, the NullMailer is also instantioted for production even though SMTP settings have been configured.

役に立ちましたか?

解決

You're not calling ActionMailer::Base#mail in MainMailer#event_registration.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top