如何使用AngularJS的单元测试框架Karma / Jasmine等待whenGET()
或expectGET()
完成?
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
});
});
});
adminController:
'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);
因此,如何强制
whenGET
在作用域函数isEnabled
之前运行?或如何等待whenGET
完成后才能执行isEnabled
?这是我尝试过的:
尝试#1:将
then
添加到whenGET
没用说那不是功能
$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();
});
});