AngularJS Testing Tips: Testing Directives

Using html2js is an ugly way to test directive :slight_smile:

First of all, the template markup isn’t frozen.

It can, and would probably change when the project grows. You still can mock $templateCache and fill it, breaking dependency.

Consider this directive:

angular.module(‘sampleDirectives’, [‘templates-main’])
.directive(‘fourthDirective’, function () {
return {
templateUrl: ‘directives/sampleTemplate.html’
};
});

In your test, you just need to do this:

$templateCache.put(“directives/sampleTemplate.html”, “myFakeContent”);

You still can test the updated scope, but right now you’re not depending of an evolving file.