What do underscores in _servicename_ mean in AngularJS tests?

In the following test example, the providerโ€™s original name is APIEndpointProvider, but for the introduction of injections and maintenance, the agreement seems to be entered with underscores wrapping it. Why is this?

'use strict'; describe('Provider: APIEndpointProvider', function () { beforeEach(module('myApp.providers')); var APIEndpointProvider; beforeEach(inject(function(_APIEndpointProvider_) { APIEndpointProvider = _APIEndpointProvider_; })); it('should do something', function () { expect(!!APIEndpointProvider).toBe(true); }); }); 

What are the agreements? Am I missing a better explanation?

+74
angularjs angularjs-service unit-testing jasmine
Mar 10 '13 at 2:00
source share
1 answer

The underscore is a convenient trick that we can use to enter a service under a different name so that we can locally assign a local variable with the same name as the service.

That is, if we could not do this, we would need to use a different name for the local service:

 beforeEach(inject(function(APIEndpointProvider) { AEP = APIEndpointProvider; // <-- we can't use the same name! })); it('should do something', function () { expect(!!AEP).toBe(true); // <-- this is more confusing }); 

$injector used in testing can simply remove the underscores to provide us with the module we need. It does nothing but reuse the same name.

More details in Angular docs

+102
Mar 10 '13 at 2:06
source share



All Articles