Enter Your Email Address to Watch This Lesson

Your link to unlock this lesson will be sent to this email address.

Unlock this lesson and all 962 of the free egghead.io lessons, plus get Angular 1.x content delivered directly to your inbox!



Existing egghead members will not see this. Sign in.

Testing Underscores

1:27 Angular 1.x lesson by

AngularJS injectors understand aliased for core injectables such as $rootScope in unit tests. If you surround your injected object with underscores _$rootScope_ it will be understood and injected into your test by Angular. This allows you to use a local alias of the same name.

Get the Code Now
click to level up

egghead.io comment guidelines

Avatar
egghead.io

AngularJS injectors understand aliased for core injectables such as $rootScope in unit tests. If you surround your injected object with underscores _$rootScope_ it will be understood and injected into your test by Angular. This allows you to use a local alias of the same name.

John Lindquist: A very common practice you'll see when testing is, when you inject something like rootScope, instead of just injecting it like I normally would, they would put an underscore in front of it and an underscore behind it.

I'm just going to do it the slow way here. What this allows you to do is that you can easily name something rootScope and assign it to _rootScope, and then you have a reference to rootScope which you can use.

Basically all that Angular's doing is stripping these out as a convenience. There's really nothing going on here. It's not some sort of different rootScope, it's the exact same rootScope, and the same thing works for the compile. I'm just going to rename this one. If I wanted to get the compiler, I can just assign it to compile, and then I can have the compiler available anywhere I want as well.

That's just a very common practice, because all these are usually used for injecting and assigning to something and then bringing this guy up so you can use it throughout your tests. You'll see this everywhere when reading other people's tests.

Let's just clean up this very last line here to make it more of the standard way of doing things.

HEY, QUICK QUESTION!
Joel's Head
Why are we asking?