Testing Simple Directives

It is important to test your custom directives in AngularJS due to the complexity of encapsulating functionality within custom HTML tags, attributes, classes, or comments. Unit tests are the best way to do this because they can cover the variety of ways the custom element can be used.

For simple directives you will need to use the $compile method to compile the object first and then the $digest() method to fire off all watches in the scope to ensure that expressions are evaluated. Also, if you are running multiple tests, you should create the module and inject the $compile and $rootScope into each test using a pretest buildup method.

To illustrate this, consider the following custom directive two-plus-two in a template: ...

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.