I am working on an AngularJS app. I want to be able to implement unit testing through this app. Currently, I'm struggling with unit testing one of my directives. At this time, I have a module setup as follows:
angular.module('my.module', [])
.controller('myCtrl', function ($scope, $element) {
// Directive specific business logic goes here
})
.directive('myDirective', function() {
return {
restrict: 'E',
transclude: true,
replace: true,
scope: {
title:'=',
state:'@'
},
templateUrl: 'myHtml.tpl.html',
controller: myCtrl
};
})
;
I have split my controller out from my directive because I need to be able to unit test the controller. This code works in the app itself. However, I run into a problem when I attempt to unit test it. I run into issues because I can't figure out how to inject the $element into the controller from a unit test. Currently, I have the following tests setup:
describe('my.module', function () {
var $scope;
var myCtrl;
beforeEach(module('myApp'));
beforeEach(inject(function ($controller, $rootScope, $element) {
$scope = $rootScope.$new();
myCtrl = $controller('myCtrl', { $scope: $scope });
}));
it('should create controller', inject(function () {
expect(testCtrl).toBeDefined();
}));
it('should create the directive', inject(function ($rootScope, $compile, $log) {
var d = angular.element('<my-directive></my-directive>');
$compile(d)($rootScope);
$scope.$digest();
expect($log.assertEmpty).not.toThrow();
}));
});
$element is something that automatically gets injected into a directive. However, I can't figure out how to inject this into a controller. I need to do this so I can unit test it. How do I do this?
Thank you!