Angular Mocks'模块'第二次运行失败

时间:2015-12-04 00:05:51

标签: angularjs unit-testing mocha chai

我试图为我创建的角度服务(工厂)编写一些单元测试。我无法运行我的测试,因为angular.mock.module函数在与angular.mock.inject函数配对时第二次调用时失败。我已经验证我的测试与这些错误无关,因为它从未进入我的第二次测试并且第一次测试通过正常。错误来自beforeEach,我重新初始化我正在测试的角度模块。

这是我的简化代码:

// service code
function daEvents ($rootScope) {
  var api = {
    on: function () {}
  };

  return api;
}

daEvents.$inject = ['$rootScope'];

angular.module('test', []).factory('daEvents', daEvents);

// test code
describe('daEvents', function () {
  beforeEach(angular.mock.module('test'));

  var daEvents;
  beforeEach(angular.mock.inject(function (_daEvents_) {
    daEvents = _daEvents_;
  }));

  it('should exist', function () {
    chai.expect(daEvents).to.exist;
  });

  it('should test ok', function () {
    chai.expect(true).to.be.true;
  });
});

我收到此错误:

Error: [ng:areq] Argument 'fn' is not a function, got string
http://errors.angularjs.org/1.5.0-beta.2/ng/areq?p0=fn&p1=not%20a%20function%2C%20got%20string
    at https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.0-beta.2/angular.js:68:12
    at assertArg (https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.0-beta.2/angular.js:1810:11)
    at assertArgFn (https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.0-beta.2/angular.js:1820:3)
    at Function.annotate (https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.0-beta.2/angular.js:3930:5)
    at Function.angular.injector.$$annotate (:425:187)
    at Object.invoke (https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.0-beta.2/angular.js:4626:36)
    at :428:256
    at Object.forEach (https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.0-beta.2/angular.js:340:20)
    at a.workFn (:428:9)
    at t (https://cdnjs.cloudflare.com/ajax/libs/mocha/2.3.4/mocha.min.js:7:33286)

请参阅此JSFiddle:

JSFiddle Example

1 个答案:

答案 0 :(得分:2)

这是一个已知问题,修正案已在:https://github.com/angular/angular.js/issues/13397