【问题标题】:Testing Angular factory that wraps the external function测试包装外部函数的 Angular 工厂
【发布时间】:2015-07-19 02:05:53
【问题描述】:

我的角度工厂包装了外部函数:

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

因果报应测试:

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

在第二个测试中,我收到错误TypeError: Cannot redefine property: $id。 是否可以不更改ExternalWrap 工厂进行测试?

【问题讨论】:

    标签: javascript angularjs unit-testing testing karma-jasmine


    【解决方案1】:

    你需要设置configurable: true,所以如果你想按原样离开那个工厂,我猜答案是“不”。

    【讨论】:

    • 谢谢。我希望可以设置 karma 以便在每次测试之前刷新页面?
    • 不确定。我建议您就 Karma 配置问题提出一个新问题。
    猜你喜欢
    • 2015-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多