2

I'm trying to run an rspec test. You can see most of that code here.

Maybe it's relevant: CoRegEmailWorker.perform contains this:

ProvisionalUser.where("unsubscribed = false AND disabled = false AND (email_sent_count < ? OR email_sent_count is NULL) AND (last_email_sent <= ? OR last_email_sent IS NULL) AND sign_up_date IS NULL",
                      ProvisionalUser::EMAIL_COUNT_LIMIT, email_sending_interval.hours.ago).
                each{ |user|
  begin
    user.send_email
  rescue Exception => ex
    logger.error ex
  end
}

and ProvisionalUser has this method:

  def send_email
    self.email_sent_count = self.email_sent_count.nil? ? 1 : self.email_sent_count + 1
    self.last_email_sent = DateTime.now
    self.disabled = true if self.email_sent_count == EMAIL_COUNT_LIMIT
    self.save!

    ProvisionalUserNotifier.send_registration_invite(self.id).deliver
  end

Finally, ProvisionalUserNotifier inherits from MailGunNotifier which inherits from ActionMailer.

The problem I'm having is that the deliveries array is not being populated. In my `config/environments/test.rb'. I have this:

config.action_mailer.perform_deliveries = true
config.action_mailer.delivery_method = :test

I'm not certain what else is needed here.

i've even gone so far as to try this:

require "spec_helper"
require "action_mailer"

describe "unsubscribe functionality" do

  pu1 = ProvisionalUser.new
  pu1.email = 'contact_me@test.com'
  pu1.partner = 'partner'
  pu1.first_name = 'joe'
  pu1.save!

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

  it "should send emails to subscribed users only" do
    unsubscribed_user = FactoryGirl.build(:unsubscribed_user)
    unsubscribed_user.save!

    subscribed_user = FactoryGirl.create(:subscribed_user)
    CoRegEmailWorker.perform
    ActionMailer::Base.deliveries.length.should == 1
    ActionMailer::Base.deliveries.first.email.should =~ subscribed_user.email
    #sent.first.email.should_not =~ unsubscribed_user.email
    #sent.first.email.should =~ subscribed_user.email
  end

  def sent
    ActionMailer::Base.deliveries
  end

end
Community
  • 1
  • 1
Ramy
  • 20,541
  • 41
  • 103
  • 153
  • Where's the bit that tries to send the email? – Frederick Cheung Jan 13 '12 at 20:06
  • see my update but basically `CoRegEmailWorker.perform` calls `user.send_email` in `ProvisionalUser` which calls `ProvisionalUserNotifier.deliver`. `ProvisionalUserNotifier` inherits from `MailGunNotifier` which inherits from `ActionMailer`. – Ramy Jan 13 '12 at 20:13
  • Anything in your log file from that `rescue Exception` block? (which I consider quite dangerous since will catch stuff like syntax errors) – Frederick Cheung Jan 13 '12 at 20:16
  • where would the log messages go from test? – Ramy Jan 13 '12 at 20:35
  • log/test.log. You could just remove the rescue to see if an exception is being raised. – Frederick Cheung Jan 13 '12 at 20:45
  • wow. that was really annoying. because the exception was being eaten, i wasn't seeing that I was missing a neccessary value for the subject of the email to work. Thanks for the help!! – Ramy Jan 13 '12 at 21:09
  • Your question helped me notice that I have misconfigured `environments/test.rb` and therefore not getting `ActionMailer::Base.deliveries` array populated. So, thanks, man. – Francisco Quintero Sep 12 '16 at 15:22

1 Answers1

0

wow. that was really annoying. because the exception was being eaten, i wasn't seeing that I was missing a neccessary value for the subject of the email to work.

Ramy
  • 20,541
  • 41
  • 103
  • 153