运行测试时不触发mixpanel-ruby事件



我使用mixpanel-ruby库发送分析事件。跟踪器在初始化器中定义:

require 'mixpanel-ruby'
$mixpanel = Mixpanel::Tracker.new(Figaro.env.mixpanel_token)
if Rails.env.development? 
  # Silence local SSL errors
  Mixpanel.config_http do |http|
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end
end
在我的控制器中,我像这样触发事件:
def resume
  @study.resume!
  redirect_to studies_url, notice: 'Study resumed'
  $mixpanel.track(current_user.id, 'Resume study')
end

这工作得很好,但是当我用rspec spec运行测试套件时,事件被解雇了,这并不理想,因为它消耗了我的每月事件津贴,更不用说扭曲数据了。

我怎样才能防止这些在测试环境中触发?

要么使用代理(puffingbilly等)来模拟mixpanel服务,要么使用自定义消费者(如

)进行配置
$mixpanel = if Rails.env.test?
    Mixpanel::Tracker.new(Figaro.env.mixpanel_token) do |type, message|
        # You can just discard the track in here if you'd like, or you can
        # put it somewhere if you want to test your tracking
        # track_log << [type, message]
    end
else
    Mixpanel::Tracker.new(Figaro.env.mixpanel_token)   # Use the default if we're not testing
end
从https://github.com/mixpanel/mixpanel-ruby/issues/35