다음은 단위 테스트를 시도하고있는 컨트롤러의 일부 코드입니다.하지만별로 운이 없습니다. 오류 : spyOn에서 account()에 대한 감시 대상 개체를 찾을 수 없습니다.라는 오류 메시지가 계속 나타납니다.
컨트롤러
$scope.confirmDelete = function (account) {
var modalInstance = $modal.open({
templateUrl: '/app/accounts/views/_delete.html',
controller: function (global, $scope, $modalInstance, account) {
$scope.account = account;
$scope.delete = function (account) {
global.setFormSubmitInProgress(true);
accountService.deleteAccount(global.activeOrganizationId, account.entityId).then(function() {
global.setFormSubmitInProgress(false);
$modalInstance.close();
},
function (errorData) {
global.setFormSubmitInProgress(false);
});
};
$scope.cancel = function() {
global.setFormSubmitInProgress(false);
$modalInstance.dismiss('cancel');
};
},
resolve: {
account: function() {
return account;
}
}
});
modalInstance.result.then(function (asset) {
$scope.getAll(1, 100);
}, function() {
console.log('Modal dismissed at: ' + new Date());
});
};
테스트
describe("confirmDelete() function", function() {
var controller, scope;
// sets scope of controller before each test
beforeEach(inject(function ($rootScope, _$modal_) {
scope = $rootScope.$new();
controller = $controller('AccountsController',
{
$scope: scope,
$stateParams: mockStateParams,
$state: mockState,
// below: in order to call the $modal have it be defined and send on the mock modal?
$modal: _$modal_,
//modalInstance: mockModalInstance,
global: mockGlobal,
accountService: mockAccountSrv
});
}));
beforeEach(inject(function ($modal) {
spyOn($modal, 'open');
spyOn(scope.modalInstance, "account");
}));
it("make sure modal promise resolves", function() {
scope.confirmDelete(mockAccountSrv.account);
expect($modal.open).toHaveBeenCalled();
});
});
이제 TypeError를 제외하고는 의미가 있습니다. 'undefined'는 (modalInstance.result를 평가하는) 객체가 아닙니다. 나는 내가 가짜 모의 모달을 만들어야한다고 생각하고 있었지만 아직 일을하지 못하는 것 같다. 나는 또한 오류를 없애기 위해 아래의 코드를 가지고 있었고 올바른 길에 있었습니까? mockModalInstance.result = function (promiseHandler) { return promiseHandler(); }; – ReganPerkins