Leeuwtje
Leeuwtje

Reputation: 2271

how to test constants in module?

I have the following module:

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

I would like to unit test this so I created:

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

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

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

});

Getting an error now:

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

How can I fix this?

Upvotes: 2

Views: 3893

Answers (1)

logee
logee

Reputation: 5077

You should be injecting your constant like any other service and not your module. This works for me:

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

    });

Upvotes: 6

Related Questions