I've spent quite a while banging my head against trying to override injected constants provided to modules' config functions. My code looks something like
common.constant('I18n', <provided by server, comes up as undefined in tests>);
common.config(['I18n', function(I18n) {
console.log("common I18n " + I18n)
}]);
Our usual way to guarantee what I18n is being injected in our unit tests is by doing
module(function($provide) {
$provide.constant('I18n', <mocks>);
});
This works fine for my controllers, but it seems like the config function doesn't look at what's $provided outside of the module. Instead of getting the mocked values, it gets the earlier value defined as part of the module. (Undefined in the case of our tests; in the below plunker, 'foo'.)
A working plunker is below (look at the console); does anyone know what I'm doing wrong?
First of all: it seems that jasmine is not working properly in your plunkr. But I am not quite sure – maybe someone else can check this again. Nevertheless I have created a new plunkr (http://plnkr.co/edit/MkUjSLIyWbj5A2Vy6h61?p=preview) and followed these instructions: https://github.com/searls/jasmine-all.
You will see that your
beforeEachcode will never run. You can check this:You need two things:
A real test in the
itfunction –expect(true).toBe(true)is good enoughYou must use
injectsomewhere in your test, otherwise the function provided tomodulewill not be called and the constant will not be set.If you run this code you will see "green":
I hope it will work as you expect!