2015-09-03 70 views
1

我有以下模塊:如何測試模塊中的常量?

angular.module('config', []).constant('myconstant', somevalue); 

我想單元測試這使我產生:

describe('Constants', function() { 
    var config; 

    beforeEach(inject(function (_config_) { 
    module('config'); 
    config =_config_; 
    })); 

    it('should return settings',function(){ 
    expect(config.constant('myConstant')).toEqual('somevalue'); 
    }); 

}); 

現在得到一個錯誤:

Error: [$injector:unpr] Unknown provider: configProvider <- config 

我怎樣才能解決這個問題?

+1

這是一個常數......它應該有零行爲,它不應該改變。你爲什麼要測試它?這似乎是錯誤的地方集中您的測試工作。 –

+0

你的權利傢伙我在想什麼 – Leeuwtje

回答

5

你應該注入你的常數像任何其他服務,而不是你的模塊。這適用於我:

angular.module('config',[])。constant('myconstant','somevalue');

describe('Constants', function() { 
     var myconstant; 

     beforeEach(module('config')); 

     beforeEach(inject(function (_myconstant_) { 
      myconstant =_myconstant_; 
     })); 

     it('should return settings',function(){ 
     expect(myconstant).toEqual('somevalue'); 
     }); 

    });