I'm trying to use karma to test AngularJS directives. But I'm running into issues with templateUrls. Using technique described here, it gets even stranger. It seems to work as advertised and loads my template into the $templateCache, but that cache isn't being used by the directive. Here's some code:
This will work just fine
.directive('messageComposer', function($templateCache) {
return {
restrict: 'E',
template: $templateCache.get('partials/message_composer.html'),
replace: true,
link: function() {
console.log('hello world');
}
};
});
but as soon as I use a templateUrl, it fails to bind in the tests:
.directive('messageComposer', function() {
return {
restrict: 'E',
templateUrl: 'partials/message_composer.html',
replace: true,
link: function() {
console.log('hello world');
}
};
});
Anyone know what's going on here?
Here's my unit test setup:
var $scope;
var $compile;
beforeEach(function() {
module('partials/message_composer.html');
module('messageComposer');
inject(function(_$compile_, $rootScope) {
$scope = $rootScope.$new();
$compile = _$compile_;
});
});
it("works", function() {
$scope.message = {};
elem = angular.element("<message-composer message='message'></message-composer>")
$compile(elem)($scope);
console.log(elem);
expect(true).toBeDefined();
});