使用胖箭头函数时,Jasmine注入函数失败

时间:2016-04-30 04:36:35

标签: angularjs unit-testing jasmine karma-runner

我刚开始对我的应用进行单元测试,并成功运行了一个测试套件,是的!我继续开始测试第二个控制器,一旦测试运行器(karma)遇到inject块,测试就会失败。评论它,测试再次通过。不能为我的生活找出可能发生的事情。非常基本的业力配置:

https://gist.github.com/ggoforth/82510af339370a4dca32ad4c0c4b0563

控制器看起来像:

angular.module('somemodule')
  .controller('SomeCtrl', SomeCtrl);

function SomeCtrl($rootScope, Notification) {
  $rootScope.$on('someapp.apiLimitExceeded', (evt, err) => {
    Notification.error({
      message: err.data.message
    });
  });
}

第一个测试看起来像:

describe('SomeCtrl', () => {
  let $rootScope, controller, notification;

  beforeEach(() => {
    module('mymodule');

    inject(($injector) => {
      $rootScope = $injector.get('$rootScope');
      notification = $injector.get('Notification');

      spyOn(notification, 'error');

      controller = $injector.get('$controller')('SomeCtrl', {
        $rootScope: $rootScope,
        Notification: notification 
      });
    });
  });

  describe('Initialization', () => {
    it('Should register a $rootScope listener for mymodule.apiLimitExceeded', () => {
      const args = {data: {message: 'dis an errr'}};
      $rootScope.$emit('mymodule.apiLimitExceeded', args);

      expect(notification.error).toHaveBeenCalled();
      expect(notification.error).toHaveBeenCalledWith(args.data);
      expect(notification.error.calls.count()).toBe(1);
    });
  });
});

此测试按预期通过。如果我将此文件的内容复制并粘贴到另一个测试文件中,而不更改任何内容,则测试开始失败。删除第二个测试文件会使原始测试开始通过。我必须遗漏一些关于业力如何运行这些角度测试的事情,或者一些我没有表现的泪水,但我看不出那可能是什么。有任何想法吗?我得到的错误,供参考:

TypeError: Cannot read property '1' of null
at Function.fb.$$annotate (/Users/greg/projects/someapp/client/build/js/deps.js:7451:410)
at Function.angular.injector.$$annotate (/Users/greg/projects/someapp/client/node_modules/angular-mocks/angular-mocks.js:2615:36)
at e (/Users/greg/projects/someapp/client/build/js/deps.js:7298:368)
at Object.invoke (/Users/greg/projects/someapp/client/build/js/deps.js:7299:86)
at Object.workFn (/Users/greg/projects/someapp/client/node_modules/angular-mocks/angular-mocks.js:2965:20)
at window.inject.angular.mock.inject (/Users/greg/projects/someapp/client/node_modules/angular-mocks/angular-mocks.js:2934:42)
at Object.beforeEach (/Users/greg/projects/someapp/client/test/test.again.someapp.controller.js:7:5)
Error: Declaration Location
at window.inject.angular.mock.inject (/Users/greg/projects/someapp/client/node_modules/angular-mocks/angular-mocks.js:2927:25)
at Object.beforeEach (/Users/greg/projects/someapp/client/test/test.again.someapp.controller.js:7:5)
TypeError: Cannot read property '$emit' of undefined
at Object.it (/Users/greg/projects/someapp/client/test/test.again.someapp.controller.js:23:17)

更新

问题在于我使用的是未转换的ES6,其中包括使用胖箭头函数进行注入块。显然Chrome 50/51存在问题,您必须使用正常功能。将胖箭头版本更改为正常功能可修复问题(或通过babel预处理器转换测试文件)。

0 个答案:

没有答案