Trying to write Jasmine tests for a directive that needs a parent directive to be present. Came across this question: Testing directives that require controllers, but it seems outdated (Angular 1.1.5 I think) and the plunker doesn't work with newer versions of Jasmine (2.2.1) and Angular (1.3.14). Getting this error:
TypeError: undefined is not a function
at Object.<anonymous> (http://run.plnkr.co/AHQtPtvSezB4DsnJ/appSpec.js:18:33)
The code snippet:
var app = angular.module('plunker', []);app.directive('foo', function() {
return {
restrict: 'E',
controller: function($scope) {
this.add = function(x, y) {
return x + y;
}
}
};
});
app.directive('bar', function() {
return {
restrict: 'E',
require: '^foo',
link: function(scope, element, attrs, foo) {
scope.callFoo = function(x, y) {
scope.sum = foo.add(x, y);
}
}
};
});
And the test case:
describe('Testing directives', function() {
var $scope, $compile, element;
beforeEach(function() {
module('plunker');
inject(function($rootScope, _$compile_) {
$scope = $rootScope.$new();
$compile = _$compile_;
});
});
it('checks if callFoo works correctly', function() {
// Arrange
var element = $compile('<foo><bar></bar></foo>')($scope);
var fooController = element.controller('foo');
var barScope = element.find('bar').scope();
spyOn(fooController, 'add').andReturn(3);
// Act
barScope.callFoo(1, 2);
// Assert
expect(barScope.sum).toBe(3);
expect(fooController.add).toHaveBeenCalledWith(1, 2);
});
});
Here's the updated plunker:
http://plnkr.co/edit/bC5BY81x0hi576xwSdyo?p=preview
Realize this may not be the ideal design, but currently stuck with it. Thanks!