Mau*_*boa 5 javascript unit-testing jasmine angularjs angularjs-service
我正在尝试对使用$ http的服务进行单元测试.我正在使用Jasmine并且我继续收到此错误:
TypeError:解析在angular.js中未定义(第13737行)
这就是我的服务:
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;
}]);
Run Code Online (Sandbox Code Playgroud)
这是我的测试看起来像:
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();
});
});
});
Run Code Online (Sandbox Code Playgroud)
我对基于angularjs的应用程序的单元测试非常陌生,我在angularjs文档中使用了这个例子
https://docs.angularjs.org/api/ngMock/service/ $ httpBackend
我不确定我可能缺少什么,我已经尝试了不同的东西,我总是得到同样的错误,任何帮助将不胜感激.
该parsed变量是相关服务的URL.这是undefined出于以下原因之一:
token 没有定义sucess并且error回调没有data.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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
参考
| 归档时间: |
|
| 查看次数: |
3301 次 |
| 最近记录: |