vendredi 29 janvier 2016

Unit testing a helper inside a custom provider in angularjs

I have this provider:

angular.module('app').provider('appProvider', function() {
  this.$get = Helper;

  function Helper() {
    function method() {
      return true;
    };

    return {
      method: method
    };
});

When unit testing it, I can reach appProvider, but not the Helper in unit tests. I've been trying this:

describe('test', function() {
  var prov;

  beforeEach(angular.mock.module('app', function(appProvider) {
    prov = appProvider;
  }));

  it('provider', inject(function() {
    expect(prov.Helper.method()).toEqual(true);
  }));
});

And getting this error:

TypeError: 'undefined' is not an object (evaluating 'prov.Helper.method()')

Question is: How do I reach method() in order to evaluate is correct behaviour?

Aucun commentaire:

Enregistrer un commentaire