2012-06-08 13 views
5

Sto provando a testare alcuni mailer con rspec ma le consegne sono sempre vuote. Qui è la mia prova rspec:ActionMailer :: Base.deliveries sempre vuoto in Rspec

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 

non riesce con:

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

La mia funzione send_welcome_email assomiglia a questo (che è il mio modello):

def send_welcome_email 
    AccountMailer.welcome self 
end 

E il mio 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 

Qualche idea? Non sono sicuro di come procedere.

risposta

14

Hai provato che funziona mentre stai eseguendo l'app? Forse il tuo test è corretto per non riuscire.

Ho notato che non si sta mai chiamando deliver quando si crea il messaggio, quindi sospetto che il test non funzioni perché l'email non viene inviata. Mi aspetto il metodo send_welcome_email a guardare più come

def send_welcome_email 
    AccountMailer.welcome(self).deliver 
end 
+0

solo mi ha salvato un sacco di tempo. grazie per questo! – MilesStanfield

Problemi correlati