How can I check the AngularJS provider?

I need to test my own angular provider, and I need to test it both at the configuration stage and at the startup stage to check if the configuration methods work and that the created provider is really configured with the correct parameters.

When I ask about dependency injection for the provider, it cannot find APIResourceFactoryProvider, only APIResourceFactory, and I did not find examples of this in the repositories that I have looked through so far.

+10
angularjs unit-testing jasmine
Mar 13 '13 at 16:58
source share
3 answers

This is actually much simpler than it would be to first check the provider in AngularJS:

describe('Testing a provider', function() { var provider; beforeEach(module('plunker', function( myServiceProvider ) { provider = myServiceProvider; })); it('should return true on method call', inject(function () { expect( provider.method() ).toBeTruthy(); })); }); 

`` ``

The proof is in Plunker: http://plnkr.co/edit/UkltiSG8sW7ICb9YBZSH

+20
Apr 05 '13 at 7:38
source share

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); })); // the trick is here beforeEach(inject(function($injector) { storageService = $injector.invoke(provider.$get); })); it('should return Html5 storage service being run in a usual browser', function () { expect(storageService).toBe(html5); }); }); 

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.

+1
Sep 07 '16 at 16:44
source share

here is a small helper that correctly encapsulates vendor samples, therefore, provides isolation between individual tests:

  /** * @description request a provider by name. * IMPORTANT NOTE: * 1) this function must be called before any calls to 'inject', * because it itself calls 'module'. * 2) the returned function must be called after any calls to 'module', * because it itself calls 'inject'. * @param {string} moduleName * @param {string} providerName * @returns {function} that returns the requested provider by calling 'inject' * usage examples: it('fetches a Provider in a "module" step and an "inject" step', function() { // 'module' step, no calls to 'inject' before this var getProvider = providerGetter('module.containing.provider', 'RequestedProvider'); // 'inject' step, no calls to 'module' after this var requestedProvider = getProvider(); // done! expect(requestedProvider.$get).toBeDefined(); }); * it('also fetches a Provider in a single step', function() { var requestedProvider = providerGetter('module.containing.provider', 'RequestedProvider')(); expect(requestedProvider.$get).toBeDefined(); }); */ function providerGetter(moduleName, providerName) { var provider; module(moduleName, [providerName, function(Provider) { provider = Provider; }]); return function() { inject(); return provider; }; // inject calls the above } 
  • the provider retrieval process is fully encapsulated: there is no need for closure variables that compromise isolation between tests.
  • the process can be divided into two steps: the “module” step and the “injection” step, which can be appropriately grouped with other “module” and “injection” calls in the unit test.
  • If separation is not required, obtaining a provider can be done with only one command!
0
Apr 22 '15 at 22:19
source share



All Articles