Ruby on rails 如何使用RSpec测试Rails 3.2 ActionMailer是否呈现了正确的视图模板?

Ruby on rails 如何使用RSpec测试Rails 3.2 ActionMailer是否呈现了正确的视图模板?,ruby-on-rails,rspec,actionmailer,rspec-rails,Ruby On Rails,Rspec,Actionmailer,Rspec Rails,我正在使用,我想测试我的邮件程序是否呈现了正确的视图模板 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(

我正在使用,我想测试我的邮件程序是否呈现了正确的视图模板

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 "cs@mycompany.com"

      # I would like to test here something like
      # ***** HOW ? *****
      expect(template_path).to eq("mailers/my_mailer/notify_customer")
    end
  end
end
这是一种有效的方法吗?或者我应该做些完全不同的事情

更新

MyMailer#notify_customer
可能有一些逻辑(例如,根据客户的区域设置)在不同情况下选择不同的模板。控制器在不同的情况下呈现不同的视图样板,这或多或少是一个类似的问题。使用
RSpec
可以编写

expect(response).to render_template "....." 

它是有效的。我正在为邮递员寻找类似的东西。

好的,我理解你现在想要实现的目标

您应该能够通过在邮件程序上设置使用特定参数调用的
mail
方法的期望值来测试调用了哪个模板

在测试中尝试以下方法:

MyMailer.should_receive(:mail).with(hash_including(:template => 'expected_template'))

我认为这离上面的答案更近了一步,因为它确实测试隐式模板

#重要!
#必须复制https://gitlab.com/gitlab-org/gitlab/-/blob/master/spec/support/helpers/next_instance_of.rb
它“呈现foo_-mail”do
允许(描述的类)do | mailer的下一个实例|
允许(邮件)接收(:呈现到正文)和包装原始do,选项|
expect(选项[:模板])到eq('foo_-mail')
m、 看涨期权
结束
结束
body=subject.body.encoded
结束

为什么要测试它?视图路径由mailer方法的名称定义。这里几乎没有什么地方会出错。@DominikGoltermann请参阅以上我的更新否,我不想测试特定模板的内容。正如您所说,这是视图规范的观点。请看我的更新。在某些情况下,
操作基于某种逻辑呈现不同的视图模板。我想测试这个逻辑。我想能够测试这个(呈现哪个模板)。请简要说明一下这里给出的解决方案:我认为这不适用于呈现的模板是隐式的情况(默认情况下,它呈现与操作名称匹配的模板)。