I have created a common ModalService
and this is used for two different type of dialogs. CancelDialog
and ErrorDialog
will be popped up as per parameter passed to service.
i.e This will show an ErrorDialog
ModalService.openModal('Analysis Error', 'I am Error Type', 'Error');
Unit test for resolve
is failing. Check this PLUNKER for running Unit Test.
This is in file ModalDialogService.js
. See sample code here:
function openCancelModal(title, message, callback) {
$uibModal.open({
templateUrl: 'CancelDialog.html',
controller: 'DialogController',
controllerAs: 'vm',
backdrop: 'static',
size: 'md',
resolve: {
message: function() {
return message;
},
title: function() {
return title;
},
callback: function() {
return callback;
}
}
});
}
This is test file ModalService.spec.js
describe('ModalService', function() {
var $injector;
var $uibModal;
// inject the module of your controller
beforeEach(module('validationApp', function($provide) {
$uibModal = {
open: jasmine.createSpy('open')
};
$provide.value('$uibModal', $uibModal);
}));
beforeEach(inject(function(_$injector_) {
$injector = _$injector_;
}));
it('tests that openErrorModal is called', function() {
var modalService = $injector.get('ModalService');
modalService.openModal(null, null, "Error");
expect($uibModal.open).toHaveBeenCalledWith(jasmine.objectContaining({
controller: "DialogController",
templateUrl: 'ErrorDialog.html',
resolve: {
message: function() {
return message;
},
title: function() {
return title;
},
callback: function() {
return callback;
}
}
}));
});
it('tests that openCancelModal is called', function() {
var modalService = $injector.get('ModalService');
modalService.openModal(null, null, "Cancel");
expect($uibModal.open).toHaveBeenCalledWith(jasmine.objectContaining({
controller: "DialogController",
templateUrl: 'CancelDialog.html'
}));
});
});
Failing Error
Expected spy open to have been called with [ <jasmine.objectContaining(Object({ controller: 'DialogController', templateUrl: 'ErrorDialog.html', resolve: Object({ message: Function, title: Function, callback: Function }) }))> ] but actual calls were [ Object({ templateUrl: 'ErrorDialog.html', controller: 'DialogController', controllerAs: 'vm', backdrop: 'static', size: 'md', resolve: Object({ message: Function, title: Function, callback: Function }) }) ].
I found this ANSWER helpful but not able to replicate.
How to cover unit test for resolve
with vm
style?