In one of my controllers, I have implemented a simple $scope.$on function:
app.controller('MyController', function($scope) {
$scope.$on("myBroadRcvr", function(event, data) {
$scope.name = data.name;
$scope.empID = data.empID;
});
});
To test this functionality, I attempted unit testing in the following manner:
describe('MyController', function () {
var $scope, super_controller;
beforeEach(function(){
module('myApp');
inject(function($rootScope, $controller){
$scope = $rootScope.$new();
spyOn($scope, '$on');
super_controller = $controller('MyController', {$scope: $scope});
});
});
it('should successfully receive name as "test"', function (){
var myData = {name: 'test', empID: 'eg7gg'},
var sub_scope = $scope.$new();
sub_scope.$broadcast('myBroadRcvr', myData);
expect($scope.$on).toHaveBeenCalled();
expect($scope.name).toBe('test');
});
});
However, when running the test, the following error occurred:
Expected undefined to be 'test'.