rspec testing- delayed job, after_create

167 views
Skip to first unread message

Sandy Y.

unread,
Jul 27, 2014, 2:38:11 PM7/27/14
to rs...@googlegroups.com

rspec testing of sending email as delayed job in Rails 4, help with reviewing test spec and deciphering rspec error message.  I'm working backwards here where I have working code but the test I am writing for it is not passing:

I'm carrying out a unit test on my model:

class Inbound_Email < ActiveRecord::Base
belongs_to :user
after_create :send_confirmation_email

private

def :send_confirmation_email
# create delay job that sends a confirmation to the user who wrote in 
# confirmation_email is the email template to be used 
UserMailer.delay.confirmation_email(self.id)
end

end

---------------------------------------------
here is my rspec file

require 'spec_helper'

describe Inbound_Email do 
before(:each) do
Delayed::Worker.delay_jobs = false
message = FactoryGirl.create(:message)
end

it "queues confirmation mail job when a inbound email is received" do 
mock_delay = double('mock_delay').as_null_object
UserMailer.any_instance.stub(:delay).and_return(mock_delay)
mock_delay.should_receive(:send_confirmation_email)
Delayed::Job.count.should == 1
end

end
_________________________________________

here is my factory girl file for message:

FactoryGirl.define do

  factory :message do

    user

    to ["mail@#{EMAIL_URI}"]

    from 'ma...@mail.com'

    subject 'email subject'

    raw_html ''

    raw_text ''

end

-----------------------------------------------------------------------------------

I am getting the following error when running the spec:
Failure/Error: message = FactoryGirl.create(:message)
ArgumentError:
An SMTP To address is required to send a message. Set the message smtp_envelope_to, to, cc, or bcc address.
# ./app/models/user.rb:54:in `send_welcome_email'
# ./spec/models/message_spec.rb:6:in `block (2 levels) in <top (required)>

----------------------------------------------------------------------------------------------

I have googled the error message but there weren't any helpful info online. Am I testing correctly? and if so, how do I pass the test?

Kris Leech

unread,
Aug 13, 2014, 5:26:49 AM8/13/14
to rs...@googlegroups.com
warning: contains shameless self promotion

This isn't directly addressing your question, but you might want to look at something like the Wisper gem, it allows you to decouple the sending of the email from the `User` model, which means you can test the different parts in isolation.

In general `after_save` callbacks in models will make testing more difficult. The trade off is you move away from the Rails golden path.
Reply all
Reply to author
Forward
0 new messages