gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 00:04:21 25 4
gpt4 key购买 nike

如何使用 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
});
});

});

管理员 Controller :

'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);

AFTER 运行此行:

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

那么,我如何强制 whenGET 在范围函数 isEnabled 之前运行? 或者我如何等​​到 whenGET 完成后我可以执行 isEnabled?

这是我尝试过的:


尝试 #1:将 then 添加到 whenGET

没用。 said 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();
});
});

关于javascript - 如何等待 "whenGET"完成使用 AngularJS/Karma 单元测试框架?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39086157/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com