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

标签 angularjs karma-runner restangular httpbackend

我正在使用 Karma 对我的 AngularJS 应用程序进行单元测试,并使用 $HttpBackend 对后端进行 stub 。不知何故,flush() 方法似乎并没有解决我的所有请求,因为 Controller 中的一些变量仍然未定义。但是,如果我在解决我的期望之前添加超时,它工作正常!

我的 Controller :

    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,
        $location,
        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 调用正在访问实际服务器而不是模拟,这将导致您需要超时。

关于angularjs - $httpBackend.flush() 仍然需要超时来定义对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19023963/

相关文章:

javascript - 如何使用 lodash _.filter 与 _.forEach 重构 for 循环?

jasmine - 如何通过 karma-jasmine 指定 `random:true`

javascript - RestAngular:put 和 customPUT 正在发送旧对象,而不是更新对象

angularjs - 如何使用 CORS 以 Angular 上传文件

javascript - Angular if 条件不读取 ng-show 的 substr 条件

css - 同一容器行中的对象出现在彼此下方

javascript - Angular 按钮的问题

angularjs - 对具有依赖项的 AngularJS 工厂进行单元测试

Angularjs e2e 测试过去/ future 的比较

javascript - Angular.js - 无法注入(inject)外部模块