我有一个 AngularJS 服务,服务名称是 User。我用它来进行身份验证。在这个服务中,我有一个 authenticate() 函数,它使用 Restangular 对我们的 API 进行 REST 调用。它在我的 Angular 应用程序中完美运行。我刚开始使用业力,所以我可以包括测试。我想知道为什么我会收到这个错误。此外,我阅读了一些 stackoverflow 帖子并注意到他们直接在他们的规范文件中使用 $httpBackend。不幸的是,它与我的设置不同。
INFO [watcher]: Changed file "/Users/admin/repos/acme/acme-ui/test/spec/controllers/main.js".
Chrome 41.0.2272 (Mac OS X 10.8.3) App: CockpitApplication factory: GlobalPermissionsFactory should resolve status to 200 FAILED
Error: Unexpected request: POST https://someinternaldomain.com/v1/authentokens
No more request expected
at $httpBackend (/Users/admin/repos/acme/acme-ui/app/bower_components/angular-mocks/angular-mocks.js:1224:9)
at sendReq (/Users/admin/repos/acme/acme-ui/app/bower_components/angular/angular.js:9538:9)
at serverRequest (/Users/admin/repos/acme/acme-ui/app/bower_components/angular/angular.js:9255:16)
at processQueue (/Users/admin/repos/acme/acme-ui/app/bower_components/angular/angular.js:13075:27)
at /Users/admin/repos/acme/acme-ui/app/bower_components/angular/angular.js:13091:27
at Scope.$eval (/Users/admin/repos/acme/acme-ui/app/bower_components/angular/angular.js:14291:28)
at Scope.$digest (/Users/admin/repos/acme/acme-ui/app/bower_components/angular/angular.js:14107:31)
at Object.<anonymous> (/Users/admin/repos/acme/acme-ui/test/spec/controllers/main.js:71:24)
Firefox 34.0.0 (Mac OS X 10.8) App: CockpitApplication factory: GlobalPermissionsFactory should resolve status to 200 FAILED
Error: Unexpected request: POST https://someinternaldomain/v1/authentokens
No more request expected in /Users/admin/repos/acme/acme-ui/app/bower_components/angular-mocks/angular-mocks.js (line 1224)
$httpBackend@/Users/admin/repos/acme/acme-ui/app/bower_components/angular-mocks/angular-mocks.js:1224:1
sendReq@/Users/admin/repos/acme/acme-ui/app/bower_components/angular/angular.js:9538:1
$http/serverRequest@/Users/admin/repos/acme/acme-ui/app/bower_components/angular/angular.js:9255:16
processQueue@/Users/admin/repos/acme/acme-ui/app/bower_components/angular/angular.js:13075:27
scheduleProcessQueue/<@/Users/admin/repos/acme/acme-ui/app/bower_components/angular/angular.js:13091:27
$RootScopeProvider/this.$get</Scope.prototype.$eval@/Users/admin/repos/acme/acme-ui/app/bower_components/angular/angular.js:14291:16
$RootScopeProvider/this.$get</Scope.prototype.$digest@/Users/admin/repos/acme/acme-ui/app/bower_components/angular/angular.js:14107:15
@/Users/admin/repos/acme/acme-ui/test/spec/controllers/main.js:71:13
Chrome 41.0.2272 (Mac OS X 10.8.3): Executed 4 of 4 (1 FAILED) (0.114 secs / 0.111 secs)
Firefox 34.0.0 (Mac OS X 10.8): Executed 4 of 4 (1 FAILED) (0.101 secs / 0.092 secs)
TOTAL: 2 FAILED, 6 SUCCESS
这是我的 main.js,Karma 规范文件
describe('App: CockpitApplication', function () {
// load the controller's module
beforeEach(module('CockpitApplication'));
var $controller, $q, $rootScope;
beforeEach(inject(function(_$controller_, _$q_, _$rootScope_) {
$controller = _$controller_;
$q = _$q_;
$rootScope = _$rootScope_;
}));
describe('factory: GlobalPermissionsFactory', function() {
var _GlobalPermissionsFactory = null,
_UserInformationFactory = null,
_User = null;
beforeEach(inject(function(GlobalPermissionsFactory) {
_GlobalPermissionsFactory = GlobalPermissionsFactory;
}));
beforeEach(inject(function(UserInformationFactory) {
_UserInformationFactory = UserInformationFactory;
}));
beforeEach(inject(function(User) {
_User = User;
}));
it('should return false', function() {
expect(_GlobalPermissionsFactory).toBeDefined()
});
it('should check if getUserInformation() exists', function() {
expect(_UserInformationFactory.getUserInformation).toBeDefined()
});
it('should return a promise', function() {
expect(_User.authenticate('admin', 'secret').then).toBeDefined();
});
it('should resolve status to 200', function () {
var data;
// set up a deferred
var deferred = $q.defer();
// get promise reference
var promise = deferred.promise;
// set up promise resolve callback
promise.then(function (response) {
console.log('data',data);
data = response.status;
});
_User.authenticate('admin', 'secret').then(function(response) {
// resolve our deferred with the response when it returns
deferred.resolve(response);
});
// force `$digest` to resolve/reject deferreds
$rootScope.$digest();
// make your actual test
expect(data).toEqual(200);
});
});
});