加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – $httpBackend.flush()仍然需要超时来定义对象

发布时间:2020-12-17 16:58:38 所属栏目:安全 来源:网络整理
导读:我正在使用Karma对我的AngularJS应用程序进行单元测试,并使用$HttpBackend对后端进行存根.不知何故,flush()方法似乎没有解决我的所有请求,因为控制器中的某些变量仍然未定义.但是,如果我在解决我的期望之前添加超时,它工作正常! 我的控制器: feedbackApp.c
我正在使用Karma对我的AngularJS应用程序进行单元测试,并使用$HttpBackend对后端进行存根.不知何故,flush()方法似乎没有解决我的所有请求,因为控制器中的某些变量仍然未定义.但是,如果我在解决我的期望之前添加超时,它工作正常!

我的控制器:

feedbackApp.controller('CompetenceCtrl',[ '$scope','$location','Restangular',function CompetenceCtrl($scope,$location,Restangular) {

        $scope.compId = null;
        $scope.index = null;

        Restangular.one('questionnaires',1).get().then(function (q) {
            $scope.questionnaire = q;

            angular.forEach($scope.questionnaire.competences,function (value,key) {
                var compTemp = new models.Competence(value);
                if (!compTemp.finished() && $scope.compId === null) {
                    $scope.compId = compTemp.id;
                    $scope.index = key;
                }
            });
            getCompetence($scope.compId);

        });

        function getCompetence(compId) {
            Restangular.one('questionnaires',1).one('competences',compId).get().then(function (c) {
                $scope.competence = c;
            });
        }
    }]);

我的规格:

'use strict';

describe('Controller: CompetenceCtrl',function () {
    //load the controller's module
    beforeEach(module('360FeedbackApp','mockQuestionnaire','mockCompetences'));

    var $httpBackend,$scope,createController;

    beforeEach(inject(function ($injector,_Restangular_,defaultQuestionnaire,defaultCompetences) {
        // Set up the mock http service responses
        $httpBackend = $injector.get('$httpBackend');
        // backend definition common for all tests
        $httpBackend.whenGET(apiUrl + '/questionnaires/1').respond(defaultQuestionnaire);

        $httpBackend.whenGET(apiUrl + '/questionnaires/1/competences/1').respond(defaultCompetences.competences[0]);
        $httpBackend.whenGET(apiUrl + '/questionnaires/1/competences/2').respond(defaultCompetences.competences[1]);
        $httpBackend.whenGET(apiUrl + '/questionnaires/1/competences/3').respond(defaultCompetences.competences[2]);
        $httpBackend.whenGET(apiUrl + '/questionnaires/1/competences/4').respond(defaultCompetences.competences[3]);
        $httpBackend.whenGET(apiUrl + '/questionnaires/1/competences/5').respond(defaultCompetences.competences[4]);

        $httpBackend.whenPUT(apiUrl + '/questionnaires/1/competences/1').respond(200);
        $httpBackend.whenPUT(apiUrl + '/questionnaires/1/competences/2').respond(200);
        $httpBackend.whenPUT(apiUrl + '/questionnaires/1/competences/3').respond(200);
        $httpBackend.whenPUT(apiUrl + '/questionnaires/1/competences/4').respond(200);
        $httpBackend.whenPUT(apiUrl + '/questionnaires/1/competences/5').respond(200);

        // Get hold of a scope (i.e. the root scope)
        $scope = $injector.get('$rootScope');
        // and the location
        $location = $injector.get('$location');
        // The $controller service is used to create instances of controllers
        var $controller = $injector.get('$controller');

        createController = function () {
            return $controller('CompetenceCtrl',{'$scope': $scope,'$location': $location,'Restangular': _Restangular_ });
        };
    }));

    afterEach(function () {
        $httpBackend.verifyNoOutstandingExpectation();
        $httpBackend.verifyNoOutstandingRequest();
    });

    it('should set the first competence',function () {
        $httpBackend.expectGET(apiUrl + '/questionnaires/1/competences/1');
        createController();
        $httpBackend.flush();
        //DO NOT UNDERSTAND WHY I NEED THIS TIMEOUT! 
        //THE TEST FAILS WITH 'undefined' IF I DONT USE IT!
        setTimeout(function() {
        expect($scope.competence).toBeDefined();
        },5000);
    });

任何帮助是极大的赞赏!

解决方法

当你有需要解决的promise(由Restangular调用上的.then()指示)时,你需要在$httpBackend.flush()之后调用$scope.$digest()来解决它们.这听起来也许你的Restangular调用正在击中实际的服务器而不是模拟,这将导致你需要超时.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读