I've encountered an issue while writing a Jasmine test case for the following Angular function. The test case failed with the message "Expected spy [object Object] to have been called".
$scope.displayTagModelPopup = function() {
var dialogOptions = {
templateUrl: 'views/mytags.html',
controller: 'TagsCtrl',
size: 'lg',
resolve: {
tagsAvailable: function() {
return $scope.availableTags;
}
}
};
ModalDialogFactory.showDialog(dialogOptions).then(function(result) {
$scope.selectedFields = [];
$scope.selectedFieldIds = [];
angular.forEach(result, function(tag) {
$scope.selectedFields.push(tag);
$scope.selectedFieldIds.push(tag.objectId);
});
});
};
My Jasmine Test case
it('should call displayTagModelPopup', function() {
var dialogOptions = {
templateUrl: 'views/mytags.html',
controller: 'TagsCtrl',
size: 'lg',
tagsAvailable: [{
objectId: "c647abc7-f651-4df6-880d-cf9fb69cdcb0",
dataFieldName: "author",
shortNamePath: "$.author",
templates: ["HaM sheet"]
}]
};
var spy = jasmine.createSpy(modalDialogFactory, 'showDialog').and.callFake(function(data) {
$scope.tags = [{
objectId: "c647abc7-f651-4df6-880d-cf9fb69cdcb0",
dataFieldName: "author",
shortNamePath: "$.author",
templates: ["HaM sheet"]
}];
return $scope.tags;
});
$scope.displayTagModelPopup();
$scope.$digest();
expect(spy).toHaveBeenCalled();
});
Encountering the error below: "Expected spy [object Object] to have been called. Error: Expected spy [object Object] to have been called."
Any insights on what might be causing the issue in my test case? Did I miss something?
Thank you in Advance!!!
Edited: Modified my Jasmine test case as shown below and now receiving a different message ''undefined' is not a function (evaluating 'ModalDialogFactory.showDialog(dialogOptions).then')'
Checked if ModelDialogFactory is defined or not but ModalDialogFactory.showDialog method is defined successfully. The test case fails only when the method '$scope.displayTagModelPopup();' is called.
it('should call displayTagModelPopup', function() {
spyOn(ModalDialogFactory, 'showDialog').and.callFake(function() {
$scope.tags = [{
objectId: "c647abc7-f651-4df6-880d-cf9fb69cdcb0",
dataFieldName: "author",
shortNamePath: "$.author",
templates: ["HaM sheet"]
}];
return $scope.tags;
});
var dialogOptions = {
templateUrl: 'views/mytags.html',
controller: 'TagsCtrl',
size: 'lg',
tagsAvailable: [{
objectId: "c647abc7-f651-4df6-880d-cf9fb69cdcb0",
dataFieldName: "author",
shortNamePath: "$.author",
templates: ["HaM sheet"]
}]
};
//expect(ModalDialogFactory).toBeDefined();
//expect(ModalDialogFactory.showDialog).toBeDefined();
$scope.displayTagModelPopup();
$scope.$digest();
});