Open dwmkerr opened 10 years ago
I made a mock of ModalService for test:
describe('some test', function () {
var $httpBackend, scope, modalServiceMock, modalInstance, someController;
beforeEach(function () {
inject(function (_$httpBackend_, $rootScope, $controller) {
$httpBackend = _$httpBackend_;
scope = $rootScope.$new();
// mock modal service
modalServiceMock = {
showModal: function (options) {
return {
then: function (callback) {
modalInstance = {
scope: {},
close: {
then: function (callback) {
this.callback = callback;
}
}
};
callback(modalInstance);
}
};
}
};
someController = $controller('controllerName', {
$scope: scope,
ModalService: modalServiceMock
});
});
it('open and close modal', function () {
scope.methodInitingModal();
// some checks here modal is showed
modalInstance.close.callback('confirm');
// make any check modal close callback has been called with passed result string
});
});
Looks great @alllx thanks for sharing this, I'll use this as a base when I write up the unit testing samples!
Note: When documenting the testing approach, also use:
It would be extremely useful to document how unit testing works with Angular Modal Service.