vendredi 29 mai 2015

how to mock provider configuration dependencies in angular

Is there a way to mock dependencies for provider configuration? I have the following module:

angular
  .module('xApp', [])
  .constant('constantOne', 'value one')
  .constant('constantTwo', 'value two')
  .provider('foo', ['constantOne', function(constantOne) {

    return {
      $get: ['constantTwo', function(constantTwo) {
        return  constantOne;
      }]
    }

  }]);

and tests for that:

describe('foo', function () {

  // load the controller's module
  beforeEach(module('xApp', function($provide) {
    $provide.constant('constantOne', 'test value one');
    $provide.constant('constantTwo', 'test value two');
  }));

  it('should show the problem', inject(function (constantOne, constantTwo, foo) {
    expect(constantOne).toBe('test value one'); // passes
    expect(constantTwo).toBe('test value two'); // passes
    expect(foo).toBe('test value one'); // fails, the value returned is 'value one'
  }));
});

The problem is with mocking constantOne - the provider 'foo' sees the original value instad of the mocked one.

I'm using angular 1.2.17 but also checked that the same behavior occurs in 1.4

Aucun commentaire:

Enregistrer un commentaire