Loading a AngularJS Directive TemplateURL into a Unit Test

Get Your Free AngularJS 2 Book Chapters

Also get the AngularJS exclusive deals and freebies direct to your inbox for FREE!

Objective

To unit test a directive which loads an external template for the HTML.

Technologies

Grunt, Karma, Jasmine, PhantomJS, AngularJS.

Note: It could be classed as an E2E test if we want to mock the $httpbackend and simulate events together with the controller (also to load the templates into the $templateCache) but for this one we’ll do a simple unit test to get it working. Also we could extend our app to use app.directives and pass that into the test. But for this test everything is under the app module.

Example:

In this example I am testing a bootstrap navbar directive to have been appended to the DOM and contain links.

Related Posts

Angularjs4u.com is not endorsed or certified by AngularJS. All AngularJS logos and trademarks displayed on this blog are property of AngularJS.
The views expressed on here are purely to help other developers use AngularJS.