Error: No more request expected with Karma using Restangular from within a Service
Error: No more request expected with Karma using Restangular from within a Service
我有一个 AngularJS 服务,服务名称是 User。我用它来进行身份验证。在这个服务中,我有一个 authenticate() 函数,它使用 Restangular 对我们的 API 进行 REST 调用。它在我的 Angular 应用程序中运行良好。我刚开始使用业力,所以我可以包括测试。我想知道为什么我会收到此错误。另外,我读过一些 Whosebug post 并注意到他们直接在他们的规范文件上使用 $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);
});
});
});
您确实需要使用 $httpBackend - 像下面这样使用它应该适合您。
During unit testing, we want our unit tests to run quickly and have no
external dependencies so we don’t want to send XHR or JSONP requests
to a real server. All we really need is to verify whether a certain
request has been sent or not, or alternatively just let the
application make requests, respond with pre-trained responses and
assert that the end result is what we expect it to be.
使用 $httpBackend 可以让您单独测试服务的方法 - 测试的成功与否取决于服务器是否必须正确执行任何操作。您还可以测试该方法在其请求中发送的内容。
如果不使用 $httpBackend,您实际上是在进行服务器调用,这可能会导致各种错误。一个猜测,但我会说你得到的错误是其中之一。
describe('App: CockpitApplication', function () {
// load the controller's module
beforeEach(module('CockpitApplication'));
var $controller, $q, $rootScope, $httpBackend;
beforeEach(inject(function(_$controller_, _$q_, _$rootScope_, _$httpBackend_) {
$controller = _$controller_;
$q = _$q_;
$rootScope = _$rootScope_;
$httpBackend = _$httpBackend_;
}));
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;
$httpBackend.whenGET('//url here').respond(200, //return something the server would);
// 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);
});
$httpBackend.flush();
// force `$digest` to resolve/reject deferreds
$rootScope.$digest();
// make your actual test
expect(data).toEqual(200);
});
});
});
我有一个 AngularJS 服务,服务名称是 User。我用它来进行身份验证。在这个服务中,我有一个 authenticate() 函数,它使用 Restangular 对我们的 API 进行 REST 调用。它在我的 Angular 应用程序中运行良好。我刚开始使用业力,所以我可以包括测试。我想知道为什么我会收到此错误。另外,我读过一些 Whosebug post 并注意到他们直接在他们的规范文件上使用 $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);
});
});
});
您确实需要使用 $httpBackend - 像下面这样使用它应该适合您。
During unit testing, we want our unit tests to run quickly and have no external dependencies so we don’t want to send XHR or JSONP requests to a real server. All we really need is to verify whether a certain request has been sent or not, or alternatively just let the application make requests, respond with pre-trained responses and assert that the end result is what we expect it to be.
使用 $httpBackend 可以让您单独测试服务的方法 - 测试的成功与否取决于服务器是否必须正确执行任何操作。您还可以测试该方法在其请求中发送的内容。
如果不使用 $httpBackend,您实际上是在进行服务器调用,这可能会导致各种错误。一个猜测,但我会说你得到的错误是其中之一。
describe('App: CockpitApplication', function () {
// load the controller's module
beforeEach(module('CockpitApplication'));
var $controller, $q, $rootScope, $httpBackend;
beforeEach(inject(function(_$controller_, _$q_, _$rootScope_, _$httpBackend_) {
$controller = _$controller_;
$q = _$q_;
$rootScope = _$rootScope_;
$httpBackend = _$httpBackend_;
}));
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;
$httpBackend.whenGET('//url here').respond(200, //return something the server would);
// 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);
});
$httpBackend.flush();
// force `$digest` to resolve/reject deferreds
$rootScope.$digest();
// make your actual test
expect(data).toEqual(200);
});
});
});