Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I test mandrill api with rspec

So my client has reported that many of the emails are going to the wrong person, and I would like to write some feature tests to find and make sure that they are receiving the email and what it says in my specs. I have mandrill_mailer which uses mandril api, and before it sends out I would like to see what the message is.

For example. Create a new user account -> creates the user, and then sends out a welcome email. in devise it calls RegistrationMailer.new_registration(resource).deliver which then sends a email to the user:

  def new_registration(user)
user = User.find_by_email(user["email"])
mandrill_mail template: 'new-registration',
subject: 'Welcome to ContentBlvd!',
to: { email: user["email"], name: user["full_name"] },
vars: {
  'first_name' =>   user["full_name"],
  'unsubscribe' =>  "#{CONFIG[:protocol]}#{CONFIG[:host]}/unsubscribe?email=#{user.email}"
}

end

In my mailer how do I test this mail object?
I tried ActionMailer::Base.deliveries, but it returns nil (Since I'm using mandrill mailer...) So I tried - MandrillMailer::TemplateMailer.message But no luck.... Thanks for the help.

like image 251
tspore Avatar asked Oct 22 '13 02:10

tspore


2 Answers

At this point, MandrillMailer appears to support a robust offline testing set of options. In particular, MandrillMailer.deliveries can now be examined for expectations or debugging purposes.

like image 52
Jim Van Fleet Avatar answered Nov 15 '22 06:11

Jim Van Fleet


As per wiki you need to mock Excon

# spec/rails_helper.rb

config.before(:all) do 
  Excon.defaults[:mock] = true
  Excon.stub({}, {body: "{\"html\":\"RESULT\"}", status: 200})
end
like image 42
vladiim Avatar answered Nov 15 '22 08:11

vladiim