[rspec-users] Spec'ing ActionMailer

Craig Demyanovich cdemyanovich at gmail.com
Mon Feb 5 14:22:04 EST 2007

On Feb 5, 2007, at 1:52 PM, s.ross wrote:

> David--
> I still don't have this working (see my previous email with the
> pastie attached), but now have another ActionMailer/rSpec question.
> I'm spec'ing a different mailer model directly (instead of through
> the controller). It's meant to be invoked through script/runner, so I
> won't have a response object.


Your comments got me thinking about this again. First, there is the  
behavior that you expect your controller to exhibit: that certain  
actions send email. Second, there is the behavior that your  
ActionMailer::Base derivative should exhibit: that it creates email  
as you like. Furthermore, you write that you want to invoke an  
ActionMailer::Base derivative via script/runner instead of or in  
addition to a controller.

Therefore, you should specify their respective behaviors  
independently of one another. I'm including a code snippet from  
_Agile Web Development with Rails, 2nd ed._, that shows an example of  
testing the ActionMailer::Base derivative by itself. I'm sure you can  
translate the test to a spec.

require File.dirname(__FILE__) + '/../test_helper'
require 'order_mailer'

class OrderMailerTest < Test::Unit::TestCase
   def setup
     @order = Order.new(:name =>"Dave Thomas", :email =>  
"dave at pragprog.com")

  def test_confirm
    response = OrderMailer.create_confirm(@order)
    assert_equal("Pragmatic Store Order Confirmation", response.subject)
    assert_equal("dave at pragprog.com", response.to[0])
    assert_match(/Dear Dave Thomas/, response.body)

Taking this approach will allow you to minimize the number of tests  
that you write to prove that the controller behaves as it should.  
Then, you can focus your attention on the ActionMailer::Base  
derivative alone for all the things you want to verify about the  
content of email. I hope this helps somehow.


More information about the rspec-users mailing list