A custom ModalService
has been created to display two different types of dialogs, CancelDialog
and ErrorDialog
, based on the parameter passed to the service.
For example, the following code will show an ErrorDialog
:
ModalService.openModal('Analysis Error', 'I am Error Type', 'Error');
The unit test for the resolve
function is failing. You can view the running Unit Test in this PLUNKER.
This functionality is defined in the file ModalDialogService.js
. Here is a snippet of the code:
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;
}
}
});
}
The corresponding test file is named 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'
}));
});
});
An error is occurring in the testing phase:
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 }) }) ].
If you need further assistance, you may find this ANSWER helpful in resolving the issue around covering unit tests for the resolve
function using the vm
style.