samedi 18 juillet 2015

Testing Angular factory that wraps the external model, and uses Object.defineProperty

My angular factory wraps the external object:

var External = function(){};

angular.module('app', [])
  .factory('ExternalWrap', function() {
    Object.defineProperty(External.prototype, '$id', {
      get: function() {
        return this.$$id === undefined || this.$$id === null ? this.id : this.$$id;
      },
      set: function(value) {
        this.$$id = value;
      },
      configurable: false,
      enumerable: false
    });

    return External;
  });

karma-jasmine tests:

describe('test', function () {
  beforeEach(module('app'));

  it('should work', function() {
    inject(function(ExternalWrap) {
      expect(ExternalWrap).toBeDefined();
    });
  });

  it('should work too', function() {
    inject(function(ExternalWrap) {
      expect(ExternalWrap).toBeDefined();
    });
  });
});

In the second test I get an error TypeError: Cannot redefine property: $id. Is it possible to test without changing the ExternalWrap factory?

Aucun commentaire:

Enregistrer un commentaire