angular
.module('poop', [])
.constant('foo', 1)
.provider('bar', class BarProvider {
constructor(foo) {
this.foo = foo;
}
$get(foo) {
return { foo };
}
})
.constant('baz', {})
.config((foo, baz) => {
baz.foo = foo;
});
describe('mocking constants', () => {
describe('mocking constants: part 1 (what you can and can’t do out of the box)', () => {
beforeEach(module('poop'));
it('should work in the run phase', () => {
module($provide => {
$provide.constant('foo', 2);
});
inject(foo => {
expect(foo).toBe(2);
});
});
it('...which includes service instantiations', () => {
module($provide => {
$provide.constant('foo', 2);
});
inject(bar => {
expect(bar.foo).toBe(2);
});
});
it('should work in the config phase, technically', () => {
module($provide => {
$provide.constant('foo', 2);
});
module(foo => {
// Code passed to ngMock module is effectively an added config block.
expect(foo).toBe(2);
});
inject();
});
it('...but only if that config is registered afterwards!', () => {
module($provide => {
$provide.constant('foo', 2);
});
inject(baz => {
// Earlier we used foo in a config block that was registered before the
// override we just did, so it did not have the new value.
expect(baz.foo).toBe(1);
});
});
it('...and config phase does not include provider instantiation!', () => {
module($provide => {
$provide.constant('foo', 2);
});
module(barProvider => {
expect(barProvider.foo).toBe(1);
});
inject();
});
});
describe('mocking constants: part 2 (why a second module may not work)', () => {
// We usually think of there being two lifecycle phases, 'config' and 'run'.
// But this is an incomplete picture. There are really at least two more we
// can speak of, ‘registration’ and ‘provider instantiations’.
//
// 1. Registration — the initial (usually) synchronous calls to module methods
// that define services. Specifically, this is the period prior to app
// bootstrap.
// 2. Provider preparation — unlike the resulting services, which are only
// instantiated on demand, providers whose recipes are functions will all
// be instantiated, in registration order, before anything else happens.
// 3. After that is when the queue of config blocks runs. When we supply
// functions to ngMock module, it is effectively like calling
// module.config() (likewise calling `inject()` is like adding a run block)
// so even though we can mock the constant here successfully for subsequent
// config blocks, it’s happening _after_ all providers are created and
// after any config blocks that were previously queued have already run.
// 4. After the config queue, the runtime injector is ready and the run queue
// is executed in order too, so this will always get the right mocks. In
// this phase (and onward) services are instantiated on demand, so $get
// methods (which includes factory and service recipes) will get the right
// mock too, as will module.decorator() interceptors.
// So how do we mock a value before previously registered config? Or for that
// matter, in such a way that the mock is available to providers?
// Well, if the consumer is not in the same module at all, you can overwrite
// the whole module, as others have proposed. But that won’t work for you if
// the constant and the config (or provider constructor) were defined in app
// code as part of one module, since that module will not have your override
// as a dependency and therefore the queue order will still not be correct.
// Constants are, unlike other recipes, _unshifted_ into the queue, so the
// first registered value is always the one that sticks.
angular
.module('local-mock', [ 'poop' ])
.constant('foo', 2);
beforeEach(module('local-mock'));
it('should still not work even if a second module is defined ... at least not in realistic cases', () => {
module((barProvider) => {
expect(barProvider.foo).toBe(1);
});
inject();
});
});
describe('mocking constants: part 3 (how you can do it after all)', () => {
// If we really want to do this, to the best of my knowledge we’re going to
// need to be willing to get our hands dirty.
const queue = angular.module('poop')._invokeQueue;
let originalRecipe, originalIndex;
beforeAll(() => {
// Queue members are arrays whose members are the name of a registry,
// the name of a registry method, and the original arguments.
originalIndex = queue.findIndex(([ , , [ name ] ]) => name === 'foo');
originalRecipe = queue[originalIndex];
queue[originalIndex] = [ '$provide', 'constant', [ 'foo', 2 ] ];
})
afterAll(() => {
queue[originalIndex] = originalRecipe;
});
beforeEach(module('poop'));
it('should work even as far back as provider instantiation', () => {
module(barProvider => {
expect(barProvider.foo).toBe(2);
});
inject();
});
});
describe('mocking constants: part 4 (but be sure to include the teardown)', () => {
// But that afterAll is important! We restored the initial state of the
// invokeQueue so that we could continue as normal in later tests.
beforeEach(module('poop'));
it('should only be done very carefully!', () => {
module(barProvider => {
expect(barProvider.foo).toBe(1);
});
inject();
});
});
});
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<script>document.write('<base href="' + document.location + '" />');</script>
<link href="style.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.5.2/jasmine.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.5.2/jasmine-html.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.5.2/boot.js"></script>
<script src="https://code.angularjs.org/1.6.0-rc.2/angular.js"></script>
<script src="https://code.angularjs.org/1.6.0-rc.2/angular-mocks.js"></script>
<script src="app.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.5.2/jasmine.css">
</head>
<body>
</body>
</html>