javascript - Karma 测试对 scope.function 内函数的调用

标签 javascript angularjs jasmine karma-runner karma-jasmine

我有一个 UploadDocumentCtrl.js,我正在其中访问对另一个函数内的函数的服务调用。此外部函数绑定(bind)到范围。我的问题是我无法访问此(代码块 C)中的代码语句。我想访问“标志”变量并检查它是否为真。谁能指出我正确的方向或告诉我我在这里做错了什么?谢谢..

上传文档Ctrl.js

(function () {
    'use strict';

    angular.module('myApp')
           .controller('UploadDocumentsCtrl', UploadDocumentsCtrl);

    UploadDocumentsCtrl.$inject = ['$rootScope', '$scope', '$modalInstance', '$window', 'companyService'];

    function UploadDocumentsCtrl($rootScope, $scope, $modalInstance, $window, companyService) {

        $scope.onFileSelect = onFileSelect;
        $scope.buttonDisabled = false;



        function onFileSelect(files) {
            //Can access the code here
            function upload(file) {
            //Can access the code here as well
                companyService.uploadDocuments(file)
                    .success(function (data, status, headers, config) {
                   // Code block C 
                  // Cannot access any code here or the error code block below
                        $scope.flag = true;
                    })
                    .error(function (data, status, headers, config) {                        
                    });
            }

            files.forEach(function (file) {
                file.progress = 0;
                file.percent = 0;

                $scope.filesToUpload.push(file);
                upload(file);
            });            
        }
    }
})();

Jasmine 测试用例

(function () {
"use strict";
    describe('UploadDocumentsCtrl', function () {
        var scope, companyService,companyControllerFactory;

        beforeEach(angular.mock.module('myApp'));

        beforeEach(inject(function($rootScope, $controller , _companyService_) {
                companyService = _companyService_;
                scope = $rootScope.$new();             
                companyControllerFactory = function(){$controller('UploadDocumentsCtrl',
                            {
                                $scope: scope,
                                companyService: _companyService_
                            });
            };
        }));

        describe("onFileSelect", function() {

            it(" Should make the flag to true ", function() {
                var files = [{}];
                companyControllerFactory();
                spyOn(companyService, 'uploadDocuments').and.returnValue({ success: function(){}});
                scope.onFileSelect(files);
                expect(scope.flag).toBe(true);
            });
        });
    });
})();

我在尝试执行上述操作时遇到的错误..

1) Should make the flag to true UploadDocumentsCtrl onFileSelect TypeError: companyService.uploadDocuments(...).success(...) is undefined in http://localhost:9876/absoluteC:/Users /Documents/fle/Fle/WebApiRole/app/company/UploadDocumentsCtrl.js?f11d5dcacbf2ca1d63778bfa04c582862e325523 ( line 31) upload@http://localhost:9876/absoluteC:/Users/Documents/fle/Fle/WebApiRole/app/company/UploadDocumentsCtrl .js?f11d5dcacbf2ca1d63778bfa04c582862e325523:31:17 onFileSelect/<@http://localhost:9876/absoluteC:/Users/Documents/fle/Fle/WebApiRole/app/company/UploadDocum entsCtrl.js?f11d5dcacbf2ca1d63778bfa04c582862e325523:51:17 onFileSelect@http://localhost:9876/absoluteC:/Users/Documents/fle/Fle/WebApiRole/app/company/UploadDocumen tsCtrl.js?f11d5dcacbf2ca1d63778bfa04c582862e325523:46:13 @http://localhost:9876/base/test/company/UploadDocumentsCtrlSpec.js?c5db561e203bdfae1a6f7509347d3f7032e8f785:35:17

最佳答案

在我的项目中,我使用以下格式来监视服务。您可以尝试以下选项:

var deffered = q.defer();
                spyOn(companyService, 'uploadDocuments').and.returnValue(deffered.promise);
                deffered.resolve();

要应用它,您必须在断言之前(即在 expect() 之前)使用 $rootScope.$apply()

关于javascript - Karma 测试对 scope.function 内函数的调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34133853/

相关文章:

node.js - Bluebird、Jasmine 和异步函数未处理的拒绝错误

javascript - ExtJS - Ext.Msg 框动态高度和宽度

javascript - 一起使用 TypeScript 和 Vuejs 时的对象级变量?

javascript - 如何在 HTML 标签中获取动态变量的值

javascript - 在 AngularJS ng-submit 中,如何在表单提交后绑定(bind) View ?

angularjs - 使用 Jasmine 在 Angular JS 中编写单元测试用例

javascript - es6类和多级继承

javascript - 将 $.proxy 与动态创建的元素一起使用

database - 阻止 Angular 从数据库中列出所有内容?

jasmine - 在 Karma-Runner 中使用 Karma-Jasmine 提供程序时将 Jasmine 测试标记为已跳过?