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: ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access