如何在业力茉莉花单元测试中验证模拟响应

时间:2017-08-02 05:55:11

标签: angularjs karma-jasmine

我需要验证服务成功和错误状态后的响应。我的服务如下。

   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');
        });

    };

我需要验证index,supportsDocuments变量。 我已经创建了一个模拟如下。

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

我该怎么做?

1 个答案:

答案 0 :(得分:0)

您创建了一个间谍对象,但没有创建模拟响应,您要做的是创建一个返回模拟值的间谍。

你想模仿你的诺言的回应,并在解决(然后)和拒绝(错误)时做出断言(期望)。

此帖子中有一些示例Unit-test promise-based code in Angular