[rspec-users] specs for ActionMailer

Jonathan Linowes jonathan at parkerhill.com
Tue May 29 14:52:44 EDT 2007


Hi,

Email notifications are an important part of my app and I would like  
to spec them.  Here's what I have going so far. It seems to work. I'd  
appreciate comments/suggestions.

Rails script/generate mailer produces tests for the email contents  
etc. I've started by manually converting the generated unit tests to  
rspec, and then changing/adding examples as I implement the emails.

In this scenario, an AdminNotify message is sent when a create action  
of PagesController class completes.

-----------------
app/models/admin_notify.rb :

class AdminNotify < ActionMailer::Base

   def itemcreated(item, sent_at = Time.now)
     @subject    = 'MySite: itemcreated'
     @body       = { :item => item }
     @recipients = 'admin at gmail.com'
     @from       = 'mysite at gmail.com'
     @sent_on    = sent_at
     @sent_on    = sent_at
     @headers    = {}
   end

-----------------
app/views/admin_notify/itemcreated.text.html.erb :

<p><b>AdminNotify#itemcreated</b></p>
<p>A new item was created</p>
<p>Class: <%= @item.class %><br />
Name: <%= @item.name %></p>

-----------------
spec/fixtures/admin_notify/itemcreated :

<p><b>AdminNotify#itemcreated</b></p>
<p>A new item was created</p>
<p>Class: SitePage<br />
Name: asdfasdf</p>

-----------------
spec/models/admin_notify_spec.rb :

require File.dirname(__FILE__) + '/../spec_helper'

FIXTURES_PATH = File.dirname(__FILE__) + '/../fixtures'
CHARSET = "utf-8"

include ActionMailer::Quoting

describe AdminNotify do

   before(:each) do
     ActionMailer::Base.delivery_method = :test
     ActionMailer::Base.perform_deliveries = true
     ActionMailer::Base.deliveries = []

     @item = SitePage.new( :name => 'asdfasdf')
     @item.save!
     @expected = TMail::Mail.new
     @expected.set_content_type "text", "plain", { "charset" =>  
CHARSET }
     #@expected.set_content_type "multipart", "alternative",  
{ "charset" => CHARSET }
     @expected.mime_version = '1.0'
     @expected.from = 'jonathan at linowes.com'
     @expected.to = 'linojon at gmail.com'
   end

   it "should generate correct 'create notify' message" do
     @expected.subject = 'ReviewRamp: itemcreated'
     @expected.body    = read_fixture('itemcreated')
     @expected.date    = Time.now

     # (asserting mimeparts is too hard )
     #@expected.encoded.should == AdminNotify.create_itemcreated 
(@item, at expected.date).encoded
     # just check contents part of message
     response = AdminNotify.create_itemcreated(@item, at expected.date)
     do_check_email_response(@expected, response)

   end
end

   def read_fixture(action)
     IO.readlines("#{FIXTURES_PATH}/admin_notify/#{action}")
   end

   def encode(subject)
     quoted_printable(subject, CHARSET)
   end

   def do_check_email_response(expected, response)
     response.date.should == expected.date
     response.from.should == expected.from
     response.to.should == expected.to
     response.subject.should == @expected.subject
     response.body.should == @expected.body
   end

#end of admin_notify_spec.rb














More information about the rspec-users mailing list