Dependencies and Unit Tests

Dependencies are one of the most widely used mechanisms in AngularJS. You see them everywhere, from dependency injection to global services. This can present a problem when it comes to unit tests because you don’t really want to have to test the full functionality of the dependency within the unit test. Instead, you will want to get the dependency and control it with your own mock objects and services.

There are four methods for controlling the dependency injection:

■ Create your own instance of the dependency using the new operator.

■ Create the dependency as a global object you can look up anywhere.

■ Ask a registry for it. This option requires that you have access to the registry as well, which means also placing ...

Get Learning AngularJS now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.