2015-09-03 68 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'); 
     }); 

    });