jeudi 3 décembre 2015

Angular Mocks 'module' fails second time it is ran.

I'm trying to write some unit tests for an angular service (factory) that I have created. I can not get my tests to run because the angular.mock.module function fails the second time it is called when paired with an angular.mock.inject function. I have verified that my tests have nothing to do with these errors as it never gets into my second test and the first test passes fine. The error comes from the beforeEach where I re-initialize the angular module I am testing.

Here is my simplified code:

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

  return api;
}

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

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


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

  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;
  });
});

and I get this error:

Error: [ng:areq] Argument 'fn' is not a function, got string
http://ift.tt/1TCzyHj
    at http://ift.tt/1Ntk9rM
    at assertArg (http://ift.tt/1TCzyHn)
    at assertArgFn (http://ift.tt/1Ntk9rQ)
    at Function.annotate (http://ift.tt/1TCzAPh)
    at Function.angular.injector.$$annotate (:425:187)
    at Object.invoke (http://ift.tt/1Ntk9rW)
    at :428:256
    at Object.forEach (http://ift.tt/1TCzAPl)
    at a.workFn (:428:9)
    at t (http://ift.tt/1Ntk9s0)

See this JSFiddle:

JSFiddle Example

Aucun commentaire:

Enregistrer un commentaire