I have written test case for $scope.$on but this time I came to a little difficult scenario.
Here is the $on function:
$scope.$on('x', function (eventName, eventData) {
var details = _this.details;
if (eventData.title !== details.title) {
retu;
}
if (details.item1) {
someFactory.functionA(details.item1);
}
if (details.item2) {
someFactory.functionA(details.item2);
}
});
I want to cover test for
if (eventData.title !== details.title) {
retu;
}
Here is what I did:
it('should not call someFactory.functionA', function () {
spyOn($scope, '$on');
spyOn(someFactory, 'functionA');
$scope.item = {
title: 'aaa'
};
$scope.$digest();
var $ctrl = element.controller('someController', {$scope: $scope});
$scope.$broadcast('x', 'eventData', 'eventType');
expect(someFactory.functionA).not.toHaveBeenCalled();
});
This passing the test and covering the code. But the problem is even if we delete the someFactory.functionA() the test will pass. So I need some more efficient way to write this test. Please suggest.
Note: I can't change the code structure as it is freezed. Only test can be improved.
