请帮助我的http请求一直失败,我尝试了不同的方法来自互联网
test.js
{
describe('EcdreportController', function(){
beforeEach(function() {
module('mean');
module('mean.ecdreport');
});
// Initialize the controller and a mock scope
var EcdreportController,
$scope,
$httpBackend,
$stateParams,
$location;
beforeEach(inject(function($controller, $rootScope, _$location_, _$stateParams_, _$httpBackend_,$http) {
$scope = $rootScope.$new();
EcdreportController = $controller('EcdreportController', {
$scope: $scope,
$http : $http
});
$stateParams = _$stateParams_;
$httpBackend = _$httpBackend_;
$location = _$location_;
}));
afterEach(function() {
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
});
it('$scope.getdatamodel',function($http){
// test expected GET request
$httpBackend.expectGET('/api/v1/getdatamodel').respond(200);
//expect($httpBackend.flush).not.toThrow();
$httpBackend.flush();
$scope.getDataModel()
expect($scope.datamodel).toEqual(data)
});
})
}
controllers.js
$scope.getDataModel = function() {
$http({url:'/api/v1/getdatamodel', method:"GET"})
.success(function(data) {
console.log('Datamodel successful');
$scope.datamodel = data[0];
console.log('datamodel', data);
})
.error(function(error) {
$scope.datamodel =[];
});
}
// console.log("Trying to get datamodel");
$scope.getDataModel();`
终端结果
PhantomJS 2.1.1 (Linux 0.0.0) EcdreportController $scope.getdatamodel FAILED
Error: Unexpected request: GET /api/users/me
Expected GET /api/v1/getdatamodel in bower_components/angular-mocks/angular-mocks.js (line 1412)
$httpBackend@bower_components/angular-mocks/angular-mocks.js:1412:90
n@bower_components/angular/angular.min.js:98:461
bower_components/angular/angular.min.js:95:490
bower_components/angular/angular.min.js:130:511
$eval@bower_components/angular/angular.min.js:145:107
$digest@bower_components/angular/angular.min.js:142:178
flush@bower_components/angular-mocks/angular-mocks.js:1791:45
packages/custom/ecdreport/public/tests/ecdreport.tests.js:71:21
Error: [$rootScope:inprog] http://errors.angularjs.org/1.5.7/$rootScope/inprog?p0=%24digest in bower_components/angular/angular.min.js (line 137)
n@bower_components/angular/angular.min.js:137:402
$digest@bower_components/angular/angular.min.js:142:31
verifyNoOutstandingExpectation@bower_components/angular-mocks/angular-mocks.js:1823:45
packages/custom/ecdreport/public/tests/ecdreport.tests.js:63:46
PhantomJS 2.1.1(Linux 0.0.0):执行3 of 3(1失败)(0.056秒/0.074秒)