Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActionMailer::Base.deliveries always empty in Rspec

I'm trying to test some mailers with rspec but deliveries are always empty. Here is my rspec test:

require "spec_helper"

describe "AccountMailer", :type => :helper do
  before(:each) do
    ActionMailer::Base.delivery_method = :test
    ActionMailer::Base.perform_deliveries = true
    ActionMailer::Base.deliveries = []
  end

  it "should send welcome email to account email" do
    account = FactoryGirl.create :account
    account.send_welcome_email

    ActionMailer::Base.deliveries.empty?.should be_false
    ActionMailer::Base.deliveries.last.to.should == account.email
  end
end

It fails with:

1) AccountMailer should send welcome email to account email
     Failure/Error: ActionMailer::Base.deliveries.empty?.should be_false
        expected true to be false

My send_welcome_email function looks like this ( that's my model ):

def send_welcome_email 
    AccountMailer.welcome self
end

And my mailer:

class AccountMailer < ActionMailer::Base
  default from: APP_CONFIG['email']['from']

  def welcome data
    if data.locale == 'es'
      template = 'welcome-es'
    else
      template = 'welcome-en'
    end

    mail(:to => data.email, :subject => I18n.t('welcome_email_subject'), :template_name => template)
  end
end

Any ideas? I'm not sure about how to proceed.

like image 627
Fernando Avatar asked Jun 08 '12 16:06

Fernando


1 Answers

Have you tested that it's working when you're actually running the app? Perhaps your test is correct to be failing.

I noticed that you're never calling deliver when you create the mail, so I suspect that the test is failing because email is, in fact, not getting sent. I would expect your send_welcome_email method to look more like

def send_welcome_email 
  AccountMailer.welcome(self).deliver
end
like image 100
Emily Avatar answered Oct 19 '22 17:10

Emily