简体   繁体   English

如何使用RSpec测试Mandrill API

[英]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. 我有使用mandril api的mandrill_mailer,在发送之前,我想看看消息是什么。

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: 在设计时,它将调用RegistrationMailer.new_registration(resource).deliver,然后将电子邮件发送给用户:

  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. 我尝试了ActionMailer :: Base.deliveries,但是它返回nil(因为我使用的是mandrill邮件程序...)所以我尝试了-MandrillMailer :: TemplateMailer.message但是没有运气。...感谢您的帮助。

At this point, MandrillMailer appears to support a robust offline testing set of options. 此时,MandrillMailer似乎支持一组强大的脱机测试选项。 In particular, MandrillMailer.deliveries can now be examined for expectations or debugging purposes. 特别是,现在可以检查MandrillMailer.deliveries的期望或调试目的。

As per wiki you need to mock Excon 根据wiki,您需要模拟Excon

# spec/rails_helper.rb

config.before(:all) do 
  Excon.defaults[:mock] = true
  Excon.stub({}, {body: "{\"html\":\"RESULT\"}", status: 200})
end

You don't have to use the Mandrill gem to send emails using Mandrill. 您无需使用Mandrill gem即可使用Mandrill发送电子邮件。 Just use the Rails default Mailer and add the configuration to application.rb or production.rb to use your Mandrill's account. 只需使用Rails的默认Mailer并将配置添加到application.rb或production.rb即可使用您的Mandrill帐户。

http://help.mandrill.com/entries/21738467-Using-Mandrill-s-SMTP-integration-with-Web-Frameworks http://help.mandrill.com/entries/21738467-Using-Mandrill-s-SMTP-integration-with-Web-Frameworks

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM