【问题标题】:How to validate mock response in karma jasmine unit testing如何在 karma jasmine 单元测试中验证模拟响应
【发布时间】:2017-08-02 14:26:24
【问题描述】:

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

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

    };

我需要验证索引、supportingDocuments 变量。 我创建了一个模拟如下。

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

我该怎么做?

【问题讨论】:

    标签: angularjs karma-jasmine


    【解决方案1】:

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

    您想模拟您的承诺的响应,并对解决 (then) 和拒绝(error) 做出断言(期望)。

    这个帖子里有例子Unit-test promise-based code in Angular

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-27
      • 1970-01-01
      • 2018-05-10
      • 1970-01-01
      • 2018-02-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多