Javascript 无模拟$http的角度测试
我有一个REST客户端,它是angularJS应用程序的一部分,我想为它编写测试;我尝试使用Jasmine(在$httpBackend上使用passthrough),但根本无法让它与真正的端点通信(这是一项要求)Javascript 无模拟$http的角度测试,javascript,angularjs,unit-testing,jasmine,automated-tests,Javascript,Angularjs,Unit Testing,Jasmine,Automated Tests,我有一个REST客户端,它是angularJS应用程序的一部分,我想为它编写测试;我尝试使用Jasmine(在$httpBackend上使用passthrough),但根本无法让它与真正的端点通信(这是一项要求) 有人知道有一个明智的图书馆可以做到这一点吗?或者,一种让Jasmine提交的方法?您需要注入第一个$httpbackend describe('MyController', function() { var $httpBackend, $rootScope, createContro
有人知道有一个明智的图书馆可以做到这一点吗?或者,一种让Jasmine提交的方法?您需要注入第一个$httpbackend
describe('MyController', function() {
var $httpBackend, $rootScope, createController, authRequestHandler;
// Set up the module
beforeEach(module('MyApp'));
beforeEach(inject(function($injector) {
// Set up the mock http service responses
$httpBackend = $injector.get('$httpBackend');
// backend definition common for all tests
authRequestHandler = $httpBackend.when('GET', '/auth.py')
.respond({userId: 'userX'}, {'A-Token': 'xxx'});
// Get hold of a scope (i.e. the root scope)
$rootScope = $injector.get('$rootScope');
// The $controller service is used to create instances of controllers
var $controller = $injector.get('$controller');
createController = function() {
return $controller('MyController', {'$scope' : $rootScope });
};
$httpBackend.when('GET', "/api/rest/").respond(data_to_respond);
}));
下一步编写测试用例
it('getTypes - should return 3 car manufacturers', function () {
service.getTypes().then(function(response) {
//expect will be here
});
$httpBackend.flush();
});
您需要注入第一个$httpbackend
describe('MyController', function() {
var $httpBackend, $rootScope, createController, authRequestHandler;
// Set up the module
beforeEach(module('MyApp'));
beforeEach(inject(function($injector) {
// Set up the mock http service responses
$httpBackend = $injector.get('$httpBackend');
// backend definition common for all tests
authRequestHandler = $httpBackend.when('GET', '/auth.py')
.respond({userId: 'userX'}, {'A-Token': 'xxx'});
// Get hold of a scope (i.e. the root scope)
$rootScope = $injector.get('$rootScope');
// The $controller service is used to create instances of controllers
var $controller = $injector.get('$controller');
createController = function() {
return $controller('MyController', {'$scope' : $rootScope });
};
$httpBackend.when('GET', "/api/rest/").respond(data_to_respond);
}));
下一步编写测试用例
it('getTypes - should return 3 car manufacturers', function () {
service.getTypes().then(function(response) {
//expect will be here
});
$httpBackend.flush();
});
您可以使用带有量角器的e2e测试或其他什么来测试它。如果您只想测试后端rest API,为什么不使用Mocha呢?如果您想使用从客户端到服务器的真实http请求进行测试,那么您不需要使用$httpBackend。我用jasmine编写了一些测试,它们要求真正的端点。Jasmine支持异步测试,因此编写与真实rest api通信的测试没有问题。@MarkoCen我还没有调查过Mocha,但我会检查它,谢谢@thadam我最初没有$httpBackend,但它仍然不起作用,因为它在幕后强制使用模拟后端。你可以使用带有量角器的e2e测试或其他什么来测试它。如果你只想测试后端rest API,为什么不使用Mocha呢如果你想测试从客户端到服务器的真实http请求,那么您就不需要使用$httpBackend。我用jasmine编写了一些测试,它们要求真正的端点。Jasmine支持异步测试,因此编写与真实rest api通信的测试没有问题。@MarkoCen我还没有调查过Mocha,但我会检查它,谢谢@thadam我本来没有$httpBackend,而且它仍然不起作用,因为它在幕后强制使用模拟后端。正如我在问题中所说的,我不想伪造响应-我希望我的客户机与真实的服务器对话。在测试时不可能,你必须伪造响应。问我是否有其他测试框架,你不必,因为我确信这是Jasmine的一个限制:)首先,这不是因为你不能,我告诉你这是因为你不应该这样做,因为你违反了单元测试的原则。单元测试的主要目的是测试单个部件并快速进行测试,如果您使用http或实时数据,有时可能会失败。在这种情况下,您的测试用例将失败,并最终导致测试性能的总体结果。我知道您不打算使用单元测试进行测试,但这没关系,因为我没有编写单元测试!(我可能不应该用“单元测试”来标记这个问题)。那么,您如何获得适当的$http服务来与Jasmine一起工作呢?正如我在问题中所说的,我不想伪造响应-我希望我的客户机与真实的服务器进行对话。在测试时不可能,您必须伪造响应。我问您是否有其他不需要的测试框架,因为我确信这是Jasmine的一个限制:)首先,这不是因为你不能,我告诉你这是因为你不应该这样做,因为你违反了单元测试的原则。单元测试的主要目的是测试单个部件并快速进行测试,如果您使用http或实时数据,有时可能会失败。在这种情况下,您的测试用例将失败,并最终导致测试性能的总体结果。我知道您不打算使用单元测试进行测试,但这没关系,因为我没有编写单元测试!(我可能不应该用“单元测试”来标记这个问题)。那么,您如何获得适当的$http服务来与Jasmine一起工作呢?