【问题标题】:Unit tests breaking using AngularJS, Pusher + Karma使用 AngularJS、Pusher + Karma 打破单元测试
【发布时间】:2017-06-24 10:32:00
【问题描述】:

我正在工作的一个项目正在使用Pusher,以及Karma 进行单元测试。单元测试当前失败,引用以下错误:

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 和不使用),更改这些值都不会改变失败的单元测试错误消息。

【问题讨论】:

    标签: javascript angularjs unit-testing phantomjs pusher


    【解决方案1】:

    PhantomJS 1.9.8 目前已经很老了(2 年以上),在使用现代 Web 应用程序时可能会崩溃。

    您应该尝试 2.5 beta,其中包含最新的 Webkit 引擎:PhantomJS downloads

    【讨论】:

    • 感谢您的回复,更新了 package.json 中的 phantomjs 并运行了 npm install - 没有区别。我还需要补充一点,单元测试以前可以正常工作。
    • npm phantomjs 包的版本与 phantomjs 二进制本身的版本不同。现在是什么版本? phantomjs --version
    • 也许 PhantomJS 版本不对错误负责,但您通常最好使用该软件的较新版本。
    • 该项目在一个 vagrant box 中运行。运行 phantomjs --version 告诉我程序没有安装。 npm 文件中指定的版本是 "phantomjs": "~1.9.18",
    • 1.9.18 是 npm 模块“phantomjs”的版本,而不是它驱动的二进制应用程序“phantomjs”。我说的是升级后者。从module's page来看,可以驱动PhantomJS v2.+的较新版本称为phantomjs-prebuilt
    猜你喜欢
    • 2018-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-12
    • 1970-01-01
    • 2013-05-16
    • 1970-01-01
    相关资源
    最近更新 更多