What does a module function do in AngularJS?

Vojta Test Guidelines Testing What does this code do?

// load the tabs code beforeEach(module('tabs')); 

He says he is loading the tab code, but why? Isn't the tabs module already defined here?

  var tabs = angular.module('tabs', []); 

Can someone give an explanation in explaining what should be loaded, why and how in the angular test?

I tried calling this function in my tests, for example

  beforeEach(module('utils')) ; 

and I get this error:

 TypeError: Property 'module' of object [object Object] is not a function 

Also, when I try to load my templates like this

 beforeEach(module('templates/loadingBar.html')); 

I get this error:

 Error: No module: templates/loadingBar.html 

I really got confused in the whole process.

Thanks for the help...

+4
source share
1 answer

The code you provided

 var tabs = angular.module('tabs', []); 

creates the tabs module, but to load it, you probably put something like ng-app="tabs" on your DOM element. This instructs Angular to load the tabs module and make its definitions available to the injector , which the application uses to resolve dependencies. (For details, see the Download Guide ).

In your tests there is no ng-app directive to initialize the injector or load your module; the module function (which exists in angular.mock from the angular-mocks.js ) does this for you in tests. If you use a Karma Jasmine or Mocha adapter for testing, it makes the module available to you, otherwise you may need to call angular.mock.module .

+10
source

Source: https://habr.com/ru/post/1481871/


All Articles