Ruby on rails RSpec class_Rails邮件间谍
我试图测试在保存模型时是否使用了特定的mailer类。在我的模型中,我有:Ruby on rails RSpec class_Rails邮件间谍,ruby-on-rails,rspec,actionmailer,Ruby On Rails,Rspec,Actionmailer,我试图测试在保存模型时是否使用了特定的mailer类。在我的模型中,我有: class Foo < ActiveRecord::Base def send_email if some_condition FooMailer.welcome.deliver_now else FooBarMailer.welcome.deliver_now end end def 当我运行此测试时,失败的原因是: (ClassDouble(FooMai
class Foo < ActiveRecord::Base
def send_email
if some_condition
FooMailer.welcome.deliver_now
else
FooBarMailer.welcome.deliver_now
end
end
def
当我运行此测试时,失败的原因是:
(ClassDouble(FooMailer) (anonymous)).welcome(*(any args))
expected: 1 time with any arguments
received: 0 times with any arguments
问题似乎是您没有用spy替换mailer类的当前定义,因此spy没有收到任何消息。要替换它,请使用以下方法:
(ClassDouble(FooMailer) (anonymous)).welcome(*(any args))
expected: 1 time with any arguments
received: 0 times with any arguments
it 'uses the foo bar mailer' do
foobar_mailer = class_spy(FooBarMailer)
stub_const('FooBarMailer', foobar_mailer)
subject.send_email
# some_condition will evaluate to false here, so we'll use the FooBarMailer
expect(foobar_mailer).to have_received :welcome
end