在我的angularJS应用程序中,我使用MixPanel通过index.html中的异步脚本进行分析。

index.html


<script type="text/javascript">(function(e,b){if(!b.__SV){var a,f,i,g;window.mixpanel=b;a=e.createElement("script");a.type="text/javascript";a.async=!0;a.src=("https:"===e.location.protocol?"https:":"http:")+'//cdn.mxpnl.com/libs/mixpanel-2.2.min.js';f=e.getElementsByTagName("script")[0];f.parentNode.insertBefore(a,f);b._i=[];b.init=function(a,e,d){function f(b,h){var a=h.split(".");2==a.length&&(b=b[a[0]],h=a[1]);b[h]=function(){b.push([h].concat(Array.prototype.slice.call(arguments,0)))}}var c=b;"undefined"!== typeof d?c=b[d]=[]:d="mixpanel";c.people=c.people||[];c.toString=function(b){var a="mixpanel";"mixpanel"!==d&&(a+="."+d);b||(a+=" (stub)");return a};c.people.toString=function(){return c.toString(1)+".people (stub)"};i="disable track track_pageview track_links track_forms register register_once alias unregister identify name_tag set_config people.set people.set_once people.increment people.append people.track_charge people.clear_charges people.delete_user".split(" ");for(g=0;g<i.length;g++)f(c,i[g]);
  b._i.push([a,e,d])};b.__SV=1.2}})(document,window.mixpanel||[]);
  mixpanel.init("@@mixPanelId");
</script>


我正在使用Karma和Jasmine编写测试。
当我运行karma:unit时,出现以下错误:ReferenceError:未定义mixpanel

这是由于我在服务之一中对MixPanel进行的调用:


mixpanel.register_once({
  BrowserVersion: "#{browserInfos.browser}-#{browserInfos.version}"
})


您是否知道如何添加MixPanel以避免此错误?

最佳答案

为了模拟混合面板,我创建了一个简单的模拟:

测试/模拟/mixpanel.coffee


class MixpanelMock
  track: () ->
    console.log("mixpanel.track", arguments)
  register_once: () ->
    console.log("mixpanel.register_once", arguments)

window.mixpanel = new MixpanelMock()


然后,我将其添加到karma.conf.js中,一切正常。

关于javascript - AngularJS-使用Jasmine和mixpanel进行测试,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23785603/

10-12 15:17