如何存根响应而不是查询依赖于外部服务的$ resource?我收到一个错误:Error: Unexpected request: GET data/counties.json
这是我的服务:
angular.module('myApp.services', ['ngResource']).
factory("geoProvider", function($resource){
return {
states: $resource('data/states.json', {}, {
query: {method: 'GET', params: {}, isArray: false}
}),
counties: $resource('data/counties.json', {}, {
query: {method: 'GET', params: {}, isArray: false}
})
};
});
规格:
describe('service', function() {
beforeEach(module('myApp.services'));
describe('geoProvider', function() {
it('should return data / JSON', inject(function(geoProvider){
var data = geoProvider.counties.query();
expect(data).toBe(some);
}));
});
});
答案 0 :(得分:3)
您可以将 $ httpBackend 注入您的测试并配置它以期望您需要的请求:
describe('service', function() {
var $httpBackend;
beforeEach(module('myApp.services'));
beforeEach(inject(function(_$httpBackend_) {
$httpBackend = _$httpBackend_;
}));
it('should return data / JSON', inject(function(geoProvider){
$httpBackend.expectGET('yoururl').respond({/* the response object */});
var data = geoProvider.counties.query();
$httpBackend.flush();
expect(data).toBe(some);
}));
});