Just in case, if you want to have a virus-protected version of your provider, things get a little more complicated.
Here is the provider code:
angular .module('core.services') .provider('storageService', [function () { function isLocalStorageEnabled(window) { return true; } this.$get = ['$window', 'chromeStorageService', 'html5StorageService', function($window, chromeStorageService, html5StorageService) { return isLocalStorageEnabled($window) ? html5StorageService : chromeStorageService; }]; }]);
Test case:
describe('Storage.Provider', function() { var chrome = {engine: 'chrome'}; var html5 = {engine: 'html5'}; var storageService, provider; beforeEach(module('core.services')); beforeEach(function () { module(function (storageServiceProvider) { provider = storageServiceProvider; }); }); beforeEach(angular.mock.module(function($provide) { $provide.value('html5StorageService', html5); $provide.value('chromeStorageService', chrome); }));
In this case, $ get is an array, and you cannot just call it a regular function that provides dependencies as arguments. The solution is to use $ injector.invoke () .
It is strange that most study guides and samples missed this detail.
sergio Sep 07 '16 at 16:44 2016-09-07 16:44
source share