Skip to content

Instantly share code, notes, and snippets.

@vtanathip
Created August 26, 2013 15:37
Show Gist options
  • Save vtanathip/6342831 to your computer and use it in GitHub Desktop.
Save vtanathip/6342831 to your computer and use it in GitHub Desktop.
In Angular when you want to unit test directive with templateUrl you must compile html to js and put it in $templateCache for use it ( solution is very easy with this plugin https://github.com/ericclemmons/grunt-angular-templates )
ngtemplates: {
myApp: {
options: {
base: 'app/templates', // $templateCache ID will be relative to this folder
prepend: 'templates/', // path to your templates files ( this will add path to it )
module: {
name: 'myAppTemplate', // (Optional) Explicitly define module name
define: true // (Optional) Define new module (Default: false)
}
},
src: 'app/templates/**.html',
dest: 'app/compiled/templates.js'
}
}
describe('Testing compile angular directive with templateUrl', function() {
var scope,
elem,
compiled,
html;
//load the module
beforeEach(module('myApp'));
// load the templates
beforeEach(module('myAppTemplate'));
beforeEach(function (){
//set our view html.
html = '<numpad></numpad>';
inject(function($compile, $rootScope) {
//create a scope (you could just use $rootScope, I suppose)
scope = $rootScope.$new();
//get the jqLite or jQuery element
elem = angular.element(html);
//compile the element into a function to
// process the view.
compiled = $compile(elem);
//run the compiled view.
compiled(scope);
//call digest on the scope!
scope.$digest();
});
});
it(' should be compiled and now you can focus another behavior',function(){
//do something beautiful here. . .
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment