I am trying to unit test the function associated with the ngClick directive. At the moment it looks something like we just started in this project, and before I get to this, I want to get some kind of coverage:
vm.open = function($event) { $event.preventDefault(); $event.stopPropagation(); vm.opened = true; };
I unit test as follows:
describe('Unit: simpleSearchController', function(){ //include main module beforeEach(module('myApp')); var ctrl, scope, event ; // inject the $controller and $rootScope services // in the beforeEach block beforeEach(inject(function($controller, $rootScope){ // Create a new scope that a child of the $rootScope scope = $rootScope.$new(); // Create the controller and alias access using controllerAs ctrl = $controller('simpleSearchController as vm', { $scope: scope }); })); // unit tests it('should set vm.opened to true', function(){ event = scope.$broadcast("click"); expect(event).toBeDefined(); scope.vm.open(event); expect(event.defaultPrevented).toBeTruthy(); expect(scope.vm.opened).toBeTruthy(); }); });
When Karma runs the test, I get this error:
TypeError: $event.stopPropagation is not a function.
Any ideas?
source share