0

I need to validate the response after success and error states of a service. My service as belows.

   vm.onSupportFileDeleted = function (file) {
        DocumentService.deleteDocument(file.fileId).then(function () {
            var index = vm.supportingDocuments.indexOf(file);
            vm.supportingDocuments.splice(index, 1);
            vm.dzRemoveFile(file);
            setSupportFileStatusA11yAlert('Document removed');
            //advanced-options-link
            angular.element('#advanced-options-link').trigger('focus');
        }, function (msg) {
            $log.error('Document not deleted - ' + JSON.stringify(msg));
            showSupportingDocumentError('Unable to delete file');
            setSupportFileStatusA11yAlert('Unable to remove document');
        });

    };

I need to validate index, supportingDocuments variables. I have created a mock as below.

            DocumentServiceMock = jasmine.createSpyObj('DocumentService', [
            'deleteDocument'
        ]);

How can i do this?

1 Answers1

0

You have created a spy object but not a mock response, what you want to do is create a spy that returns a mock value.

You want to mock the response from your promise and make assertions (expect) on resolve (then) and on reject(error).

There are examples in this thread Unit-test promise-based code in Angular

anteAdamovic
  • 1,462
  • 12
  • 23