jeudi 25 juin 2015

Truly mocking providers

So basically there's no information on how to omit config/run/provider blocks on modules without doing some file load ordering hack for unit-tests. Assume the following:

// dependency chain for providers:
// aaa <- bbb <- ccc


angular.module('module0').provider('bbb', [
  'aaaProvider', 
  function (aaaProvider) {
    // aaaProvider definition
    ...
  });

angular.module('module1').provider('ccc', [
  'bbbProvider',
  function (bbbProvider) {
    // bbbProvider definition
    ...
  });

angular.module('module1').controller('controller', [
  function () {
    // controller definition
  }]);

Now assume that we are writing a unit test for controller. How would we do that?

module('module1');
inject(function ($controller, $rootScope) {
  ...
});

Oops, we have a problem. module('module1') will trigger provider definition for ccc, that has dependency on bbb, that has dependency on aaa. So unless we prevent provider definition for ccc being triggered on module1, we will running code that has nothing to do with the controller we are unit testing.

We can mock bbb by using module(function ($provide) { ... }); notation and that way we won't need to load module0 at all.

But that doesn't solve it for ccc. There's no way for me to stop it from running.

Question: Is there a way to stop module1 from running the definition of ccc provider, which has nothing to do with controller we are unit testing?

Aucun commentaire:

Enregistrer un commentaire