如何等待 "whenGET" 完成使用 AngularJS / Karma 单元测试框架?

How do I wait for "whenGET" to finish using AngularJS / Karma unit testing framework?

如何等待 whenGET()expectGET() 完成 使用 AngularJS 的单元测试框架 Karma / Jasmine?

adminController 的单元测试:

'use strict';
//SETUP
describe('spec/controllers/admin.js - adminController', function () {
        var $controller;
        var $httpBackend;
        var scope;
        var testData_admin = {
          query: {
            input: ['ADMIN']

          }

    beforeEach(module('queueManagerApp','queueManagerApp-testTemplates'));

    beforeEach(inject(function(_$controller_, _$httpBackend_) {
        $controller = _$controller_;
        scope = {};
        $httpBackend = _$httpBackend_;
        $controller('adminController',{ $scope: scope });
    }));

    // makes sure all expected requests are made by the time the test ends
    afterEach(function() {
      $httpBackend.verifyNoOutstandingExpectation();
      $httpBackend.verifyNoOutstandingRequest();
    });

//TESTS
    describe('http test ( Get user rights details -1 )', function () {
         it('admin user should have admin rights enabled (ADMIN)', function() {
             $httpBackend.whenGET('user/rights').respond(testData_admin);
             $controller('adminController',{ $scope: scope });
             var isAdmin = scope.isEnabled(testData_admin.query.input);
             $httpBackend.flush();
             expect(isAdmin).toEqual(true); //FAILING
         });
    });

});   

管理员控制器:

'use strict';

function adminController($scope, $http, $location) {

//RUN THIS SECTION FIRST
    $scope.enabledRights = [];
    $http.get('user/rights').success(function(data) {
            $scope.enabledRights = data;
    }).error(function() {
            $scope.enabledRights = [];
    });

//RUN THIS SECTION SECOND
    var isAdmin = false;
    $scope.isEnabled = function(allowedRights) {                    
        if($scope.enabledRights.length > 0){
            if($scope.enabledRights.indexOf(allowedRights) > -1){
                isAdmin = true;             
            }else{
                isAdmin = false;            
            }
         }
      return isAdmin;
    }; 
}

angular
  .module('queueManagerApp')
  .controller('adminController',
        ['$scope', '$http', '$location', '$filter', '$window', adminController]);

我运行遇到的问题是这一行:

$httpBackend.whenGET('user/rights').respond(testData_admin);

是运行在这一行之后:

var isAdmin = scope.isEnabled(testData_admin.query.input);

那么,如何在作用域函数 isEnabled 之前将 whenGET 强制为 运行? 或如何等到 whenGET 已经完成,所以我可以执行 isEnabled?

这是我尝试过的方法:


尝试 #1:将 then 添加到 whenGET

没用。表示 then 不是函数

$httpBackend.whenGET('user/rights').respond(testData_admin).then(function(){
        $controller('adminController',{ $scope: scope });
        var isAdmin = scope.isEnabled(testData_admin.query.input);
        $httpBackend.flush();
        expect(isAdmin).toEqual(true);
});

尝试 #2:使用 expectGET 而不是 whenGET

当我 运行 这个我得到 Unexpected GET Request 'user/rights'

$httpBackend.expectGET('user/rights').respond(testData_admin);
$controller('adminController',{ $scope: scope });
var isAdmin = scope.isEnabled(testData_admin.query.input);
$httpBackend.flush();
expect(isAdmin).toEqual(true); //FAILING

有人知道我该如何解决这个问题吗?

AngularJS's docs说whenGET和expectGET没有回调函数

提前致谢!

试试这个:

describe('http test ( Get user rights details -1 )', function () {
     beforeEach(function() {
        $httpBackend.whenGET('user/rights').respond(testData_admin);
        $controller('adminController',{ $scope: scope });
     });
     it('admin user should have admin rights enabled (ADMIN)', function() {
         var isAdmin = scope.isEnabled(testData_admin.query.input);
         expect(isAdmin).toEqual(true); //FAILING
         $httpBackend.flush();
     });
});