2015-07-19 37 views
0

我的角度廠包裝的外部函數:測量角工廠包裝外部函數

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工廠的情況下進行測試?

回答

2

你需要設置configurable: true,所以我想如果你想離開那個工廠,答案是'不'。

+0

Thanks.I期望可以設置因果關係,以便在每次測試之前刷新頁面? – monkey

+0

不確定。我建議你問一個關於你的Karma配置問題的新問題。 –