Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test AngularJS custom provider

Does anyone have an example of how to unit test a provider?

For example:

config.js

angular.module('app.config', [])   .provider('config', function () {     var config = {           mode: 'distributed',           api:  'path/to/api'         };      this.mode = function (type) {       if (type) {         config.isDistributedInstance = type === config.mode;         config.isLocalInstance = !config.isDistributedInstance;         config.mode = type;         return this;       } else {         return config.mode;       }     };      this.$get = function () {       return config;     };   }]); 

app.js

angular.module('app', ['app.config'])   .config(['configProvider', function (configProvider) {     configProvider.mode('local');   }]); 

app.js is using in tests and I see already configured configProvider and I can test it as a service. But how can I test the ability to configure? Or it does not need at all?

like image 211
Maxim Grach Avatar asked Feb 08 '13 11:02

Maxim Grach


People also ask

How do I run a test case in AngularJS?

Testing in AngularJS is achieved by using the karma framework, a framework which has been developed by Google itself. The karma framework is installed using the node package manager. The key modules which are required to be installed for basic testing are karma, karma-chrome-launcher ,karma-jasmine, and karma-cli.

Is AngularJS code unit testable?

AngularJS is written with testability in mind, but it still requires that you do the right thing. We tried to make the right thing easy, but if you ignore these guidelines you may end up with an untestable application.

Which one is the test entry file for the unit test?

The angular-cli configuration of karma uses the file “test. ts” as the entry point of the tests for the application.


1 Answers

I had this same question and only found a working solution in this Google Group answer and it's referenced fiddle example.

Testing your provider code would look something like this (following the code in the fiddle example and what worked for me):

describe('Test app.config provider', function () {      var theConfigProvider;      beforeEach(function () {         // Initialize the service provider          // by injecting it to a fake module's config block         var fakeModule = angular.module('test.app.config', function () {});         fakeModule.config( function (configProvider) {             theConfigProvider = configProvider;         });         // Initialize test.app injector         module('app.config', 'test.app.config');          // Kickstart the injectors previously registered          // with calls to angular.mock.module         inject(function () {});     });      describe('with custom configuration', function () {         it('tests the providers internal function', function () {             // check sanity             expect(theConfigProvider).not.toBeUndefined();             // configure the provider             theConfigProvider.mode('local');             // test an instance of the provider for              // the custom configuration changes             expect(theConfigProvider.$get().mode).toBe('local');         });     });  }); 
like image 170
Mark Gemmill Avatar answered Sep 21 '22 04:09

Mark Gemmill