Javascript TypeError:angularjs服务单元测试中未定义解析
我正在尝试对使用$http的服务进行单元测试。我正在使用Jasmine,并且不断出现以下错误: TypeError:angular.js(第13737行)中未定义解析 这就是我的服务的样子:Javascript TypeError:angularjs服务单元测试中未定义解析,javascript,angularjs,unit-testing,jasmine,angularjs-service,Javascript,Angularjs,Unit Testing,Jasmine,Angularjs Service,我正在尝试对使用$http的服务进行单元测试。我正在使用Jasmine,并且不断出现以下错误: TypeError:angular.js(第13737行)中未定义解析 这就是我的服务的样子: angular.module('myapp.services', []) .factory('inviteService', ['$rootScope', '$http', function($rootScope, $http) { var inviteService = { tok
angular.module('myapp.services', [])
.factory('inviteService', ['$rootScope', '$http', function($rootScope, $http) {
var inviteService = {
token: '',
getInvite: function(callback, errorCallback) {
$http.get('/invites/' + this.token + '/get-invite')
.success(function(data) {
callback(data);
})
.error(function(data, status, headers, config) {
errorCallback(status);
});
}
};
return inviteService;
}]);
describe ('Invite Service', function () {
var $httpBackend, inviteService, authRequestHandler;
var token = '1123581321';
beforeEach(module('myapp.services'));
beforeEach(inject(function ($injector) {
$httpBackend = $injector.get('$httpBackend');
authRequestHandler = $httpBackend.when('/invites/' + token + '/get-invite').respond({userId: 'userX'}, {'A-Token': 'xxx'});
inviteService = $injector.get('inviteService');
}));
afterEach (function () {
$httpBackend.verifyNoOutstandingExpectation ();
$httpBackend.verifyNoOutstandingRequest ();
});
describe ('getInvite', function () {
beforeEach(function () {
inviteService.token = token;
});
it ('should return the invite', function () {
$httpBackend.expectGET('/invites/' + token + '/get-invite');
inviteService.getInvite();
$httpBackend.flush();
});
});
});
这就是我的测试结果:
angular.module('myapp.services', [])
.factory('inviteService', ['$rootScope', '$http', function($rootScope, $http) {
var inviteService = {
token: '',
getInvite: function(callback, errorCallback) {
$http.get('/invites/' + this.token + '/get-invite')
.success(function(data) {
callback(data);
})
.error(function(data, status, headers, config) {
errorCallback(status);
});
}
};
return inviteService;
}]);
describe ('Invite Service', function () {
var $httpBackend, inviteService, authRequestHandler;
var token = '1123581321';
beforeEach(module('myapp.services'));
beforeEach(inject(function ($injector) {
$httpBackend = $injector.get('$httpBackend');
authRequestHandler = $httpBackend.when('/invites/' + token + '/get-invite').respond({userId: 'userX'}, {'A-Token': 'xxx'});
inviteService = $injector.get('inviteService');
}));
afterEach (function () {
$httpBackend.verifyNoOutstandingExpectation ();
$httpBackend.verifyNoOutstandingRequest ();
});
describe ('getInvite', function () {
beforeEach(function () {
inviteService.token = token;
});
it ('should return the invite', function () {
$httpBackend.expectGET('/invites/' + token + '/get-invite');
inviteService.getInvite();
$httpBackend.flush();
});
});
});
我对基于angularjs的应用程序的单元测试非常陌生,我在angularjs文档中使用了这个例子
$httpBackend
我不确定我可能会错过什么,我已经尝试了不同的事情,我总是得到相同的错误,任何帮助将不胜感激 解析的
变量是来自相关服务的URL。由于以下原因之一,它未定义:
- URL格式不正确
- 未调用$http.get
- 未定义令牌
成功
和错误
回调没有数据
.respond
.respond
不包含响应对象作为参数 describe('simple test', test);
function test()
{
it('should call inviteService and pass mock data', foo);
function foo()
{
module('myapp.services');
inject(myServiceTest);
function myServiceTest(inviteService, $httpBackend)
{
$httpBackend.expect('GET', /.*/).respond(200, 'bar');
function callback(){};
inviteService.getInvite.token = '1123581321';
inviteService.getInvite(callback, callback);
$httpBackend.flush();
expect(callback).toHaveBeenCalledOnce();
}
}
}
参考资料