我有一个REST客户端被写入我想要编写测试的angularJS应用程序的一部分;我尝试过使用Jasmine(在$ httpBackend上使用passthrough),但根本无法让它与真正的端点进行交谈(这是一项要求)。
有没有人知道一个合理的库,允许这个?或者,茉莉花摔跤的方式?
我有一个REST客户端被写入我想要编写测试的angularJS应用程序的一部分;我尝试过使用Jasmine(在$ httpBackend上使用passthrough),但根本无法让它与真正的端点进行交谈(这是一项要求)。
有没有人知道一个合理的库,允许这个?或者,茉莉花摔跤的方式?
你需要注入第一$ 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();
});
你可以用量角器或别的东西 – user3232739
端到端测试测试它为什么如果你不使用摩卡只是想测试后端休息API – MarkoCen
如果你想测试从客户端到服务器的真实http请求,那么你不需要使用$ httpBackend。我已经在茉莉花写了测试,要求真正的端点。 Jasmine支持异步测试,因此编写与真正的rest API进行通信的测试不成问题。 – thadam