Javascript 无法测试服务中modal的.then()部分-AngularJS单元测试
我需要的是捕捉。然后是服务函数中关闭或取消模式的一部分。使用下面的方法,我正在使用modal(使用FakeModal函数),控制器中的一切都正常工作,但我如何使用它在服务中进行测试 服务Javascript 无法测试服务中modal的.then()部分-AngularJS单元测试,javascript,angularjs,unit-testing,jasmine,Javascript,Angularjs,Unit Testing,Jasmine,我需要的是捕捉。然后是服务函数中关闭或取消模式的一部分。使用下面的方法,我正在使用modal(使用FakeModal函数),控制器中的一切都正常工作,但我如何使用它在服务中进行测试 服务 (function() { 'use strict'; function reviewDeletionService($uibModal, $state, $rootScope, RecordLockService, $q, Restangular) { return { openD
(function() {
'use strict';
function reviewDeletionService($uibModal, $state, $rootScope, RecordLockService, $q, Restangular) {
return {
openDeletionModal: openDeletionModal
};
function openDeletionModal(reviewDetails, reviewImpacts, unlock) {
var reviewDeletionModalInstance = $uibModal.open({
animation: true,
size: 'md',
templateUrl: "src/app/common/modals/delete-review/deleteReviewImpacts.modal.view.html",
backdrop: 'static',
controller: 'DeleteReviewImpactsModalController as drivm',
resolve: {
reviewType: function () {
return reviewDetails.reviewType;
},
reviewTitle: function () {
return reviewDetails.reviewTitle;
},
reviewImpacts: function () {
return reviewImpacts;
}
}
});
reviewDeletionModalInstance.result.then(function (data) {
openReviewReasonModal(reviewDetails, reviewImpacts, data.noImpacts, unlock);
}, function () {
if(unlock){
RecordLockService.unlockReview(reviewDetails.reviewId);
}
})
}
}
angular.module('common')
.factory('ReviewDeletionService', reviewDeletionService);
})();
测试
(function() {
'use strict';
describe("Service: ReviewDeletionService", function() {
var reviewDeletionService, $httpBackend, restangular, successHandler, errorHandler, mockAuthenticationService, q, configParams ;
var mockCurrentUser = {id: 1782, name: "One, Coordinator", roleId: [3, 10], eauthId: "coodinator1"};
var recordLockService, modal, actualOptions;
var fakeModal = {
result: {
then: function(confirmCallback, cancelCallback) {
//Store the callbacks for later when the user clicks on the OK button of the dialog
this.confirmCallBack = confirmCallback;
this.cancelCallback = cancelCallback;
}
},
close: function( item ) {
//The user clicked OK on the modal dialog, call the stored confirm callback with the selected item
this.result.confirmCallBack(item);
},
dismiss: function( type ) {
//The user clicked cancel on the modal dialog, call the stored cancel callback
this.result.cancelCallback( type );
}
};
beforeEach(module('app'));
beforeEach(module('common'));
beforeEach(inject(function(_ReviewDeletionService_, _RecordLockService_, _$httpBackend_, _Restangular_, _AuthenticationService_, $q, _configParams_, _$uibModal_) {
reviewDeletionService = _ReviewDeletionService_;
recordLockService = _RecordLockService_;
restangular = _Restangular_;
q = $q;
$httpBackend = _$httpBackend_;
modal = _$uibModal_;
mockAuthenticationService = _AuthenticationService_;
configParams = _configParams_;
spyOn(mockAuthenticationService , 'getLoggedUser').and.callFake(function() {
var deferred = q.defer();
deferred.resolve(mockCurrentUser);
return deferred.promise;
});
successHandler = jasmine.createSpy('success');
errorHandler = jasmine.createSpy('error');
}));
afterEach(function() {
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
});
describe('call openDeletionModal function',function(){
beforeEach(function () {
spyOn(modal, 'open').and.callFake(function(options){
actualOptions = options;
return fakeModal;
});
});
it('should call openDeletionModal', function() {
reviewDeletionService.openDeletionModal(review, impacts, false);
// Here I want to test.then part, I want my test to go inside .then part.
// If this was controller I would have just add
//scope.lockedReviewModalInstance.close(data);
// or. controller.lockedReviewModalInstance.close(data);
});
});
});
})();