Angularjs Unit testing – angular.mock.module and angular.mock.inject

Read the following post to why karma to get overview about unit testing with AngularJS

By default, ng module will be loaded during unit testing. But our modules should be loaded explicitly with angular.mock.module.

The angular.mock.inject function wraps the function into an injectable function. Basically, it will inject all the necessary parameters.

For example, you want to test moduleA, but it depends on other modules also, then first, you need to load them as follows

angular.mock.module('moduleA', 'moduleB', 'moduleC');

Then, You can inject the services from these modules as follows

angular.mock.inject(function(moduleAservice){
    moduleAservice.methodA();
});

If you want to mock the service, there are 3 different ways. Check the following post

http://stackoverflow.com/questions/13592259/mocking-my-own-service-in-a-unit-test

Note: angular.mock.inject and angular.mock.module is only available
for jasmine and mocha testing framework

This entry was posted in AngularJS and tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *