Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    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.



    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




    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.