我将一个模块混合到一个邮件程序中并将其添加为一个帮助程序,以便在视图中访问它。我需要测试是否从视图中调用了正确的辅助方法(以便跟踪像素包含在电子邮件中),但 Rspec 似乎不起作用:
require "spec_helper"
describe DeviseOverrideMailer do
before :each do
# Make the helper accessible.
# This approach does not work
# class MixpanelExposer; include MixpanelFacade end
# @mixpanel = MixpanelExposer.new
# This approach also does not seem to work, but was recommended here: http://stackoverflow.com/questions/10537932/unable-to-stub-helper-method-with-rspec
@mixpanel = Object.new.extend MixpanelFacade
end
describe "confirmation instructions" do
it "tells Mixpanel" do
# Neither of these work.
# DeviseOverrideMailer.stub(:track_confirmation_email).and_return('')
@mixpanel.should_receive(:track_confirmation_email).and_return('')
@sender = create(:confirmed_user)
@email = DeviseOverrideMailer.confirmation_instructions(@sender).deliver
end
end
end
邮递员:
class DeviseOverrideMailer < Devise::Mailer
include MixpanelFacade
helper MixpanelFacade
end
模块:
class MixpanelFacade
def track_confirmation_email
# Stuff to initialise a Mixpanel connection
# Stuff to add the pixel
end
end
邮件视图(HAML):
-# Other HTML content
-# Mixpanel pixel based event tracking
- if should_send_pixel_to_mixpanel?
= track_confirmation_email @resource
错误:它抱怨它无法正确初始化 Mixpanel 连接(因为缺少请求助手),这表明 .should_receive() 没有正确地存根 track_confirmation_email() 方法。我怎样才能让它正确地存根?