使用AngularJS,Pusher + Karma进行单元测试

时间:2017-02-07 08:56:12

标签: javascript angularjs unit-testing phantomjs pusher

我正在使用的项目使用PusherKarma进行单元测试。单元测试目前失败,引用了以下错误:

ReferenceError: Can't find variable: Pusher
    at runBlock (/vagrant/front/src/app/index.run.js:9)
    at invoke (/vagrant/front/bower_components/angular/angular.js:4771)
    at /vagrant/front/bower_components/angular/angular.js:4578
    at forEach (/vagrant/front/bower_components/angular/angular.js:325)
    at createInjector (/vagrant/front/bower_components/angular/angular.js:4578)
    at WorkFn (/vagrant/front/bower_components/angular-mocks/angular-mocks.js:3120)

所以我查看了index.run.js并找到了问题:

    /** @ngInject */
  function runBlock($log,$rootScope) {

    $rootScope.wf = {
      api: '/api/v1',
      dateFormat: 'dd.MM.yyyy',
      dataRefreshListeners:{}
    };


    $rootScope.wf.pusher = new Pusher('3e2d8ab440fad96e67ff', {
       cluster: 'eu',
       encrypted: true
    });

  }

我认为在函数参数中定义Pusher或在单元测试中存根它是一个简单的例子。

我找到了pusher-test-stub库,并添加了它。这修复了我以前的所有错误,但给我留下了这个错误:

PhantomJS 1.9.8 (Linux 0.0.0) ERROR
  TypeError: 'undefined' is not an object (evaluating 'PusherDefinition.Util')
  at /vagrant/front/bower_components/pusher-test-stub/dist/pusher-test-stub.js:155


[08:51:49] 'test' errored after 3.39 s
[08:51:49] Error: Failed 1 tests.

我确实开始弄乱pusher-test-stub.js文件,但认为问题不会出现在该库中。

我还查看了我的karma.conf.js文件,记录了目录路径。在任何一种情况下(使用pusher-test-stub.js和不使用),更改这些值都不会改变失败的单元测试错误消息。

1 个答案:

答案 0 :(得分:0)

PhantomJS 1.9.8目前已经很老了(2年以上),在使用现代网络应用时可能会中断。

您应该尝试使用最近的Webkit引擎的2.5 beta版:PhantomJS downloads