问题
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.
回答1:
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.
回答2:
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
回答3:
You don't have to use the Mandrill gem to send emails using Mandrill. Just use the Rails default Mailer and add the configuration to application.rb or production.rb to use your Mandrill's account.
http://help.mandrill.com/entries/21738467-Using-Mandrill-s-SMTP-integration-with-Web-Frameworks
来源:https://stackoverflow.com/questions/19508030/how-do-i-test-mandrill-api-with-rspec