ActionMailer :: Base.deliveries is always empty in RSpec

I am trying to check some mailers with rspec, but shipments 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

Failure:

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

The send_welcome_email function looks like this (my model):

def send_welcome_email 
    AccountMailer.welcome self
end

And my mail:

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 am not sure how to proceed.

+5
source share
1 answer

Have you tested that it works when you actually launch the application? Perhaps your test will be correct if you fail.

I noticed that you never call deliverwhen creating mail, so I suspect that the test fails because the email, in fact, is not sent. I expect your method to send_welcome_emailbe more like

def send_welcome_email 
  AccountMailer.welcome(self).deliver
end
+14
source

All Articles