I use the @Mark Gemmill solution and it works well, but then came across this slightly less detailed solution that eliminates the need for a fake module.
stack overflow
So,
var provider; beforeEach(module('app.config', function(theConfigProvider) { provider = theConfigProvider; })) it('tests the providers internal function', inject(function() { provider.mode('local') expect(provider.$get().mode).toBe('local'); }));
If your providers $ get method has dependencies, you can pass them manually,
var provider; beforeEach(module('app.config', function(theConfigProvider) { provider = theConfigProvider; })) it('tests the providers internal function', inject(function(dependency1, dependency2) { provider.mode('local') expect(provider.$get(dependency1, dependency2).mode).toBe('local'); }));
Or use the $ injector to create a new instance,
var provider; beforeEach(module('app.config', function(theConfigProvider) { provider = theConfigProvider; })) it('tests the providers internal function', inject(function($injector) { provider.mode('local') var service = $injector.invoke(provider); expect(service.mode).toBe('local'); }));
Both of the above will also allow you to reconfigure the provider for each individual it statement in the describe block. But if you only need to configure the provider once for several tests, you can do this,
var service; beforeEach(module('app.config', function(theConfigProvider) { var provider = theConfigProvider; provider.mode('local'); })) beforeEach(inject(function(theConfig){ service = theConfig; })); it('tests the providers internal function', function() { expect(service.mode).toBe('local'); }); it('tests something else on service', function() { ... });
james Oct 31 '14 at 11:54 2014-10-31 11:54
source share