问题描述
我正在使用 rspec-rails 并且我想测试我的邮件程序正在呈现正确的视图模板.
I am using rspec-rails and I want to test that my mailer is rendering the correct view template.
describe MyMailer do
describe '#notify_customer' do
it 'sends a notification' do
# fire
email = MyMailer.notify_customer.deliver
expect(ActionMailer::Base.deliveries).not_to be_empty
expect(email.from).to include "[email protected]"
# I would like to test here something like
# ***** HOW ? *****
expect(template_path).to eq("mailers/my_mailer/notify_customer")
end
end
end
这是一种有效的方法吗?或者我应该做一些完全不同的事情吗?
Is this a valid approach? Or shall I do something completely different to that?
更新
MyMailer#notify_customer
可能有一些逻辑(例如,取决于客户的语言环境)在不同情况下选择不同的模板.控制器在不同情况下呈现不同的视图模板或多或少是相似的问题.使用 RSpec
你可以写
MyMailer#notify_customer
might have some logic (e.g. depending on the locale of the customer) to choose different template under different circumstances. It is more or less similar problem with controllers rendering different view templates under different circumstances. With RSpec
you can write
expect(response).to render_template "....."
它的工作原理.我正在为邮寄者寻找类似的东西.
and it works. I am looking for something similar for the mailers.
推荐答案
我认为这更接近上面的答案,因为它确实测试了隐式模板.
I think this is a step closer to the answer above, since it does test for implicit templates.
# IMPORTANT!
# must copy https://gitlab.com/gitlab-org/gitlab/-/blob/master/spec/support/helpers/next_instance_of.rb
it 'renders foo_mail' do
allow_next_instance_of(described_class) do |mailer|
allow(mailer).to receive(:render_to_body).and_wrap_original do |m, options|
expect(options[:template]).to eq('foo_mail')
m.call(options)
end
end
body = subject.body.encoded
end
这篇关于如何使用 RSpec 测试 Rails 3.2 ActionMailer 是否呈现正确的视图模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!