如何在模块中测试常量?

时间:2015-09-03 01:40:13

标签: angularjs karma-jasmine

我有以下模块:

 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 :(得分:5)

你应该像任何其他服务一样注入你的常量,而不是你的模块。这对我有用:

angular.module(&#39; config&#39;,[])。常数(&#39; myconstant&#39;,&#39; somevalue&#39;);

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

      beforeEach(module('config'));

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

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

    });
相关问题