Angular $ httpBackend错误"不再需要更多请求"

时间:2016-07-20 22:18:04

标签: angularjs karma-jasmine httpbackend

以下是我的控制器,

function adminController($scope, $http, $location) {
$scope.enabledRights = [];
$http.get('user/rights').success(function(data) {
        $scope.enabledRights = data;

}).error(function() {
        $scope.enabledRights = [];
});
}

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

以下是我对上述控制器的测试用例

'use strict';

describe('Admin controller', function () {
    var $controller;
    var $httpBackend;
    var $scope;
    var testData = {
      query: {
        input: ['XYZ']

      }
    }

beforeEach(module('queueManagerApp' ));

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

describe('http test ( Get user rights details -1 )', function () {
 it('should fetch user details', function() {
     $httpBackend.when('GET', '/user/rights')
     .respond(testData);

     $controller('adminController',{ $scope: $scope });

     //$scope.isEnabled();
     $httpBackend.flush();
     expect($scope.enabledRights).toEqual(testData.query.input);
 });
});

describe('http test (get User rights - 2 )', function () {
it('should fetch user rights details',inject(function($http) {
    var $scope = {};

    $http.get('/user/rights')
         .success(function(data,status,headers,config) {
             $scope.enabledRights = data;
         }).error(function(data, status,headers,config) {
             $scope.enabledRights = "Error";
         });

    $httpBackend
     .when('http://localhost:9999/user/rights')
     .respond(200, { enabledRights : 'XYZ' });

     $httpBackend.flush();

     expect($scope.enabledRights).toEqual ( { enabledRights : 'XYZ' }    
);   
}));
});  
});  

当我运行测试用例时,我遇到错误,我无法解决此问题。请建议我解决此问题。

INFO [watcher]: Changed file "/home/madhugowda/Madhu/gitrepo/queue-    
manager-client/test/spec/controllers/admin.js".
WARN [web-server]: 404: /queuemanager/getUserName?token=undefined
WARN [web-server]: 404: /user/clientorgs
PhantomJS 1.9.8 (Linux 0.0.0) Admin controller http test ( Get user     
rights details -1 ) should fetch user details FAILED
Error: Unexpected request: GET user/rights
No more request expected
    at $httpBackend (/home/madhugowda/Madhu/gitrepo/queue-manager-  
client/bower_components/angular-mocks/angular-mocks.js:1211)
    at sendReq (/home/madhugowda/Madhu/gitrepo/queue-manager-  
client/bower_components/angular/angular.js:10334)
    at /home/madhugowda/Madhu/gitrepo/queue-manager-  
client/bower_components/angular/angular.js:10045
    at processQueue (/home/madhugowda/Madhu/gitrepo/queue-manager-
client/bower_components/angular/angular.js:14567)
    at /home/madhugowda/Madhu/gitrepo/queue-manager-   
client/bower_components/angular/angular.js:14583
    at /home/madhugowda/Madhu/gitrepo/queue-manager- 
client/bower_components/angular/angular.js:15846
    at /home/madhugowda/Madhu/gitrepo/queue-manager-
client/bower_components/angular/angular.js:15657
    at /home/madhugowda/Madhu/gitrepo/queue-manager- 
client/bower_components/angular-mocks/angular-mocks.js:1509
    at /home/madhugowda/Madhu/gitrepo/queue-manager-  
client/test/spec/controllers/admin.js:30 
PhantomJS 1.9.8 (Linux 0.0.0) Admin controller http test (get User   
rights - 2 ) should fetch user rights details FAILED
Error: Unexpected request: GET http://localhost:9999/user/rights
No more request expected
    at $httpBackend (/home/madhugowda/Madhu/gitrepo/queue-manager- 
client/bower_components/angular-mocks/angular-mocks.js:1211)
    at sendReq (/home/madhugowda/Madhu/gitrepo/queue-manager- 
client/bower_components/angular/angular.js:10334)
    at /home/madhugowda/Madhu/gitrepo/queue-manager- 
client/bower_components/angular/angular.js:10045
    at processQueue (/home/madhugowda/Madhu/gitrepo/queue-manager- 
client/bower_components/angular/angular.js:14567)
    at /home/madhugowda/Madhu/gitrepo/queue-manager- 
client/bower_components/angular/angular.js:14583
    at /home/madhugowda/Madhu/gitrepo/queue-manager-
client/bower_components/angular/angular.js:15846
    at /home/madhugowda/Madhu/gitrepo/queue-manager-
client/bower_components/angular/angular.js:15657
    at /home/madhugowda/Madhu/gitrepo/queue-manager-  
client/bower_components/angular-mocks/angular-mocks.js:1509
    at /home/madhugowda/Madhu/gitrepo/queue-manager-
client/test/spec/controllers/admin.js:50
    at invoke (/home/madhugowda/Madhu/gitrepo/queue-manager-
client/bower_components/angular/angular.js:4450)
    at workFn (/home/madhugowda/Madhu/gitrepo/queue-manager- 
client/bower_components/angular-mocks/angular-mocks.js:2404)
undefined
PhantomJS 1.9.8 (Linux 0.0.0): Executed 6 of 6 (2 FAILED) (0.002 secs    
/ 0.028 secs) 

1 个答案:

答案 0 :(得分:0)

在您添加对所模拟的特定URL的请求之前,您将收到此错误

  • 您只做过嘲笑
  • 确保您正在代码中进行特定的URL调用
  • 如果要加载某些文件(例如, readJson或require("file.json")