简体   繁体   English

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

[英]Karma testing a call to a function inside a scope.function

I have a UploadDocumentCtrl.js where i am accessing a service call to a function inside another function . 我有一个UploadDocumentCtrl.js,我正在访问另一个函数内的函数的服务调用。 This outer function is bound to the scope . 此外部函数绑定到范围。 My problem is i am unable to access the code statements inside this (Code block C) . 我的问题是我无法访问其中的代码语句(代码块C)。 I want to access the 'flag' variable and check if that is true . 我想访问'flag'变量并检查是否为真。 Can anyone point me in the correct direction or tell me what i am doing wrong here ? 任何人都可以指出我正确的方向或告诉我我在这里做错了什么? Thanks .. 谢谢 ..

UploadDocumentsCtrl.js UploadDocumentsCtrl.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 test case 茉莉花测试案例

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

The error i am getting while trying to do the above.. 我试图做上面的错误...

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 1)应该使标志为true UploadDocumentsCtrl onFileSelect TypeError:companyService.uploadDocuments(...)。success(...)在http:// localhost:9876 / absoluteC:/ Users / Documents / fle / Fle / WebApiRole中未定义/app/company/UploadDocumentsCtrl.js?f11d5dcacbf2ca1d63778bfa04c582862e325523(第31行)上传@ 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

In my project I am using below format to spy service. 在我的项目中,我使用以下格式来窥探服务。 You can try below option: 您可以尝试以下选项:

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

And to apply this you will have to use $rootScope.$apply() before assert (ie before expect()) 要应用这个,你必须在断言之前使用$ rootScope。$ apply()(即在expect()之前)

Is companyService making an http call? companyService正在进行http调用吗?

If so, you'll need to mock the response with $httpBackend and then get to the proper conditions based on the mocked response using $httpBackend.flush() . 如果是这样,你需要使用$httpBackend模拟响应,然后使用$httpBackend.flush()根据模拟的响应获得正确的条件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从$ scope.function()内刷新$ scope - Refreshing $scope from inside a $scope.function() 你如何从href调用AngularJS $ scope.Function? - How do you call an AngularJS $scope.Function from href? AngularJS-在每个$ scope.function调用中重新定义$ scope中的数组 - AngularJS - array in $scope re-defined on each $scope.function call 通过右键单击从当前或新选项卡中的href调用Angular $ scope.Function? - Call an Angular $scope.Function from href in current or new tab by right click? Angular中$ scope.Variable和$ scope.Function之间的差异? - Differnce between $scope.Variable and $scope.Function in Angular? 从Karma和Jasmine测试中调用控制器函数 - Call a controller function from Karma and Jasmine testing 角度$ scope无法正常工作。 试图在控制器$ scope和$ scope.function范围内分配对象值 - Angular $scope not working as expected. Trying to assign object values in a controller $scope and a $scope.function scope 控制器中定义的函数(javascript)与scope.function(angular函数)的区别 - Difference of function(javascript) vs scope.function(angular function) defined in controller $ scope不调用内部函数 - $scope does not call inside function 在 Karma 中测试异步延迟功能 - Testing asynchronus delay function in Karma
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM