[英]RSpec stubbing a mailer
I have a UserMailDispatcher
class whose job it is to delver mail via ActiveMailer
based on certain criteria. 我有一个
UserMailDispatcher
类,它的工作是根据某些条件通过ActiveMailer
来传递邮件。
I'm trying to test it using RSpec but am coming up short. 我正在尝试使用RSpec进行测试但是我的时间很短。 I would like to somehow stub a test mailer and see if the class properly delivers it.
我想以某种方式存根测试邮件程序,看看该类是否正确传递它。 Here's what I have so far:
这是我到目前为止所拥有的:
All my mailers inherit from ApplicationMailer: 我的所有邮件程序都从ApplicationMailer继承:
application_mailer.rb application_mailer.rb
class ApplicationMailer < ActionMailer::Base
append_view_path Rails.root.join('app', 'views', 'mailers')
end
user_mail_dispatcher_spec.rb 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
I want to test that the dispatcher can properly queue/deliver mail. 我想测试调度程序是否可以正确排队/传递邮件。 However, I cannot seem to call
UserMailer.test_mail.deliver_now
. 但是,我似乎无法调用
UserMailer.test_mail.deliver_now
。 I get missing template 'user_mailer/test_mail'
I tried adding type: :view
to the spec and using stub_template 'user_mailer/test_mail.html.erb'
, but I get the same error. 我
missing template 'user_mailer/test_mail'
我尝试添加type: :view
到规范并使用stub_template 'user_mailer/test_mail.html.erb'
,但我得到了同样的错误。
I do have a UserMailer
defined but I don't want to test any of its methods here since those are more likely to change. 我确实定义了
UserMailer
但我不想在这里测试它的任何方法,因为它们更有可能改变。
Any ideas on how to best handle this? 关于如何最好地处理这个的任何想法?
I came across this issue as well when I was testing using a DummyMailer and to get around it I just asked the mail method to return plain text like this: 当我使用DummyMailer进行测试时,我也遇到了这个问题并绕过它我只是要求mail方法返回如下的纯文本:
mail do |format|
format.text { render plain: "Hello World!" }
end
Here is the documentation for it , scroll down a little bit to find the right section. 这是它的文档 ,向下滚动一点找到正确的部分。
Add the required template and test without stubbing by using ActionMailer::Base.deliveries
. 使用
ActionMailer::Base.deliveries
添加所需的模板并在不使用存根的情况下进行测试。 Here's an example (in minitest) from the guide ( http://guides.rubyonrails.org/testing.html#testing-your-mailers ). 这是指南( http://guides.rubyonrails.org/testing.html#testing-your-mailers )的一个例子(最小)。
require 'test_helper'
class UserMailerTest < ActionMailer::TestCase
test "invite" do
# Send the email, then test that it got queued
email = UserMailer.create_invite('me@example.com',
'friend@example.com', Time.now).deliver_now
assert_not ActionMailer::Base.deliveries.empty?
# Test the body of the sent email contains what we expect it to
assert_equal ['me@example.com'], email.from
assert_equal ['friend@example.com'], email.to
assert_equal 'You have been invited by me@example.com', email.subject
assert_equal read_fixture('invite').join, email.body.to_s
end
end
Here is the way how to test that mailer is called with right arguments. 以下是如何测试使用正确参数调用邮件程序的方法。
it 'sends email' do
delivery = double
expect(delivery).to receive(:deliver_now).with(no_args)
expect(UserMailer).to receive(:test_mail)
.with('my_arguments')
.and_return(delivery)
UserMailDispatcher.my_function
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.