Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson

Already subscribed? Sign In

Autoplay

    Testing Underscores

    John LindquistJohn Lindquist

    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.

    angularjsAngularJS
    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Transcript

    Transcript

    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.

    Discuss

    Discuss