I am mixing a module into a mailer and adding it as a helper so it is accessible in the view. I need to test that the right helper method is being called from the view (so that a tracking pixel is included in an email), but Rspec doesn't seem to work:
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
The mailer:
class DeviseOverrideMailer < Devise::Mailer
include MixpanelFacade
helper MixpanelFacade
end
The Module:
class MixpanelFacade
def track_confirmation_email
# Stuff to initialise a Mixpanel connection
# Stuff to add the pixel
end
end
The mailer view (HAML):
-# Other HTML content
-# Mixpanel pixel based event tracking
- if should_send_pixel_to_mixpanel?
= track_confirmation_email @resource
The error: It complains that it can't initialise the Mixpanel connection properly (because the request helper is missing), which shows that .should_receive() is not correctly stubbing the track_confirmation_email() method out. How can I get it to stub out correctly?