【发布时间】:2015-10-23 11:56:28
【问题描述】:
我有一个UserMailDispatcher 班级,其工作是根据某些标准通过ActiveMailer 挖掘邮件。
我正在尝试使用 RSpec 对其进行测试,但效果不佳。我想以某种方式存根一个测试邮件并查看该类是否正确交付它。到目前为止,这是我所拥有的:
我所有的邮件程序都继承自 ApplicationMailer:
application_mailer.rb
class ApplicationMailer < ActionMailer::Base
append_view_path Rails.root.join('app', 'views', 'mailers')
end
user_mail_dispatcher_spec.rb
require 'rails_helper'
describe UserMailDispatcher do
class UserMailer < ApplicationMailer
def test_mail
mail
end
end
it "mails stuff" do
???
end
end
我想测试调度程序是否可以正确排队/传递邮件。但是,我似乎无法致电UserMailer.test_mail.deliver_now。我得到missing template 'user_mailer/test_mail' 我尝试将type: :view 添加到规范并使用stub_template 'user_mailer/test_mail.html.erb',但我得到了同样的错误。
我确实定义了UserMailer,但我不想在这里测试它的任何方法,因为这些方法更有可能发生变化。
关于如何最好地处理这个问题的任何想法?
【问题讨论】:
-
您是否有特定的原因要存根,或者您只是想知道邮件已发送?另外,是否有某些原因您无法实现所需的模板?邮件操作通常有一个关联的模板。
-
我只是想知道所有必需的邮件都已发送。我喜欢只实现测试模板的想法,它可以工作(谢谢!!),但这似乎是一个非常老套的解决方案。我觉得更好的方法是以某种方式存根模板。
-
你会在生产中使用
test_mail操作吗?
标签: ruby-on-rails ruby rspec actionmailer