fix($resource): to work with $http, $httpBackend services

Breaks Disabling $resource caching for the moment.
This commit is contained in:
Vojta Jina 2011-10-19 10:47:17 -07:00 committed by Igor Minar
parent fe633dd0cf
commit feacf608ee
3 changed files with 142 additions and 147 deletions

View file

@ -36,8 +36,8 @@ Route.prototype = {
} }
}; };
function ResourceFactory(xhr) { function ResourceFactory($http) {
this.xhr = xhr; this.$http = $http;
} }
ResourceFactory.DEFAULT_ACTIONS = { ResourceFactory.DEFAULT_ACTIONS = {
@ -107,11 +107,11 @@ ResourceFactory.prototype = {
} }
var value = this instanceof Resource ? this : (action.isArray ? [] : new Resource(data)); var value = this instanceof Resource ? this : (action.isArray ? [] : new Resource(data));
self.xhr( var future = self.$http({
action.method, method: action.method,
route.url(extend({}, extractParams(data), action.params || {}, params)), url: route.url(extend({}, extractParams(data), action.params || {}, params)),
data, data: data
function(status, response) { }).on('success', function(response, status) {
if (response) { if (response) {
if (action.isArray) { if (action.isArray) {
value.length = 0; value.length = 0;
@ -123,9 +123,10 @@ ResourceFactory.prototype = {
} }
} }
(success||noop)(value); (success||noop)(value);
}, });
error || action.verifyCache,
action.verifyCache); if (error) future.on('error', error);
return value; return value;
}; };

View file

@ -3,15 +3,14 @@
/** /**
* @ngdoc object * @ngdoc object
* @name angular.module.ng.$resource * @name angular.module.ng.$resource
* @requires $xhr.cache * @requires $http
* *
* @description * @description
* A factory which creates a resource object that lets you interact with * A factory which creates a resource object that lets you interact with
* [RESTful](http://en.wikipedia.org/wiki/Representational_State_Transfer) server-side data sources. * [RESTful](http://en.wikipedia.org/wiki/Representational_State_Transfer) server-side data sources.
* *
* The returned resource object has action methods which provide high-level behaviors without * The returned resource object has action methods which provide high-level behaviors without
* the need to interact with the low level {@link angular.module.ng.$xhr $xhr} service or * the need to interact with the low level {@link angular.module.ng.$http $http} service.
* raw XMLHttpRequest.
* *
* @param {string} url A parameterized URL template with parameters prefixed by `:` as in * @param {string} url A parameterized URL template with parameters prefixed by `:` as in
* `/user/:username`. * `/user/:username`.
@ -57,7 +56,7 @@
* 'remove': {method:'DELETE'}, * 'remove': {method:'DELETE'},
* 'delete': {method:'DELETE'} }; * 'delete': {method:'DELETE'} };
* *
* Calling these methods invoke an {@link angular.module.ng.$xhr} with the specified http method, * Calling these methods invoke an {@link angular.module.ng.$http} with the specified http method,
* destination and parameters. When the data is returned from the server then the object is an * destination and parameters. When the data is returned from the server then the object is an
* instance of the resource class `save`, `remove` and `delete` actions are available on it as * instance of the resource class `save`, `remove` and `delete` actions are available on it as
* methods with the `$` prefix. This allows you to easily perform CRUD operations (create, read, * methods with the `$` prefix. This allows you to easily perform CRUD operations (create, read,
@ -128,7 +127,7 @@
* The object returned from this function execution is a resource "class" which has "static" method * The object returned from this function execution is a resource "class" which has "static" method
* for each action in the definition. * for each action in the definition.
* *
* Calling these methods invoke `$xhr` on the `url` template with the given `method` and `params`. * Calling these methods invoke `$http` on the `url` template with the given `method` and `params`.
* When the data is returned from the server then the object is an instance of the resource type and * When the data is returned from the server then the object is an instance of the resource type and
* all of the non-GET methods are available with `$` prefix. This allows you to easily support CRUD * all of the non-GET methods are available with `$` prefix. This allows you to easily support CRUD
* operations (create, read, update, delete) on server-side data. * operations (create, read, update, delete) on server-side data.
@ -201,8 +200,8 @@
</doc:example> </doc:example>
*/ */
function $ResourceProvider() { function $ResourceProvider() {
this.$get = ['$xhr.cache', function($xhr){ this.$get = ['$http', function($http) {
var resource = new ResourceFactory($xhr); var resource = new ResourceFactory($http);
return bind(resource, resource.route); return bind(resource, resource.route);
}]; }];
} }

View file

@ -1,19 +1,14 @@
'use strict'; 'use strict';
xdescribe("resource", function() { describe("resource", function() {
var resource, CreditCard, callback; var resource, CreditCard, callback;
function nakedExpect(obj) { function nakedExpect(obj) {
return expect(angular.fromJson(angular.toJson(obj))); return expect(angular.fromJson(angular.toJson(obj)));
} }
beforeEach(inject( beforeEach(inject(function($http) {
function($provide) { resource = new ResourceFactory($http);
$provide.value('$xhr.error', jasmine.createSpy('xhr.error'));
$provide.factory('$xhrError', ['$xhr.error', identity]);
},
function($xhr) {
resource = new ResourceFactory($xhr);
CreditCard = resource.route('/CreditCard/:id:verb', {id:'@id.key'}, { CreditCard = resource.route('/CreditCard/:id:verb', {id:'@id.key'}, {
charge:{ charge:{
method:'POST', method:'POST',
@ -24,6 +19,10 @@ xdescribe("resource", function() {
}) })
); );
afterEach(inject(function($httpBackend) {
$httpBackend.verifyExpectations();
}));
it("should build resource", function() { it("should build resource", function() {
expect(typeof CreditCard).toBe('function'); expect(typeof CreditCard).toBe('function');
expect(typeof CreditCard.get).toBe('function'); expect(typeof CreditCard.get).toBe('function');
@ -33,162 +32,183 @@ xdescribe("resource", function() {
expect(typeof CreditCard.query).toBe('function'); expect(typeof CreditCard.query).toBe('function');
}); });
it('should default to empty parameters', inject(function($browser) { it('should default to empty parameters', inject(function($httpBackend) {
$browser.xhr.expectGET('URL').respond({}); $httpBackend.expect('GET', 'URL').respond({});
resource.route('URL').query(); resource.route('URL').query();
})); }));
it('should ignore slashes of undefinend parameters', inject(function($browser) { it('should ignore slashes of undefinend parameters', inject(function($httpBackend) {
var R = resource.route('/Path/:a/:b/:c'); var R = resource.route('/Path/:a/:b/:c');
$browser.xhr.expectGET('/Path').respond({});
$browser.xhr.expectGET('/Path/1').respond({}); $httpBackend.when('GET').then('{}');
$browser.xhr.expectGET('/Path/2/3').respond({}); $httpBackend.expect('GET', '/Path');
$browser.xhr.expectGET('/Path/4/5/6').respond({}); $httpBackend.expect('GET', '/Path/1');
$httpBackend.expect('GET', '/Path/2/3');
$httpBackend.expect('GET', '/Path/4/5/6');
R.get({}); R.get({});
R.get({a:1}); R.get({a:1});
R.get({a:2, b:3}); R.get({a:2, b:3});
R.get({a:4, b:5, c:6}); R.get({a:4, b:5, c:6});
})); }));
it('should correctly encode url params', inject(function($browser) { it('should correctly encode url params', inject(function($httpBackend) {
var R = resource.route('/Path/:a'); var R = resource.route('/Path/:a');
$browser.xhr.expectGET('/Path/foo%231').respond({});
$browser.xhr.expectGET('/Path/doh!@foo?bar=baz%231').respond({}); $httpBackend.expect('GET', '/Path/foo%231').respond('{}');
$httpBackend.expect('GET', '/Path/doh!@foo?bar=baz%231').respond('{}');
R.get({a: 'foo#1'}); R.get({a: 'foo#1'});
R.get({a: 'doh!@foo', bar: 'baz#1'}); R.get({a: 'doh!@foo', bar: 'baz#1'});
})); }));
it('should not encode @ in url params', inject(function($browser) { it('should not encode @ in url params', inject(function($httpBackend) {
//encodeURIComponent is too agressive and doesn't follow http://www.ietf.org/rfc/rfc3986.txt //encodeURIComponent is too agressive and doesn't follow http://www.ietf.org/rfc/rfc3986.txt
//with regards to the character set (pchar) allowed in path segments //with regards to the character set (pchar) allowed in path segments
//so we need this test to make sure that we don't over-encode the params and break stuff like //so we need this test to make sure that we don't over-encode the params and break stuff like
//buzz api which uses @self //buzz api which uses @self
var R = resource.route('/Path/:a'); var R = resource.route('/Path/:a');
$browser.xhr.expectGET('/Path/doh@fo%20o?!do%26h=g%3Da+h&:bar=$baz@1').respond({}); $httpBackend.expect('GET', '/Path/doh@fo%20o?!do%26h=g%3Da+h&:bar=$baz@1').respond('{}');
R.get({a: 'doh@fo o', ':bar': '$baz@1', '!do&h': 'g=a h'}); R.get({a: 'doh@fo o', ':bar': '$baz@1', '!do&h': 'g=a h'});
})); }));
it('should encode & in url params', inject(function($browser) { it('should encode & in url params', inject(function($httpBackend) {
var R = resource.route('/Path/:a'); var R = resource.route('/Path/:a');
$browser.xhr.expectGET('/Path/doh&foo?bar=baz%261').respond({}); $httpBackend.expect('GET', '/Path/doh&foo?bar=baz%261').respond('{}');
R.get({a: 'doh&foo', bar: 'baz&1'}); R.get({a: 'doh&foo', bar: 'baz&1'});
})); }));
it("should build resource with default param", inject(function($browser) { it('should build resource with default param', inject(function($httpBackend) {
$browser.xhr.expectGET('/Order/123/Line/456.visa?minimum=0.05').respond({id:'abc'}); $httpBackend.expect('GET', '/Order/123/Line/456.visa?minimum=0.05').respond({id: 'abc'});
var LineItem = resource.route('/Order/:orderId/Line/:id:verb', {orderId: '123', id: '@id.key', verb:'.visa', minimum:0.05}); var LineItem = resource.route('/Order/:orderId/Line/:id:verb',
var item = LineItem.get({id:456}); {orderId: '123', id: '@id.key', verb:'.visa', minimum: 0.05});
$browser.xhr.flush(); var item = LineItem.get({id: 456});
$httpBackend.flush();
nakedExpect(item).toEqual({id:'abc'}); nakedExpect(item).toEqual({id:'abc'});
})); }));
it("should build resource with action default param overriding default param", inject(function($browser) { it("should build resource with action default param overriding default param", inject(function($httpBackend) {
$browser.xhr.expectGET('/Customer/123').respond({id:'abc'}); $httpBackend.expect('GET', '/Customer/123').respond({id: 'abc'});
var TypeItem = resource.route('/:type/:typeId', {type: 'Order'}, var TypeItem = resource.route('/:type/:typeId', {type: 'Order'},
{get: {method: 'GET', params: {type: 'Customer'}}}); {get: {method: 'GET', params: {type: 'Customer'}}});
var item = TypeItem.get({typeId:123}); var item = TypeItem.get({typeId: 123});
$browser.xhr.flush();
nakedExpect(item).toEqual({id:'abc'}); $httpBackend.flush();
nakedExpect(item).toEqual({id: 'abc'});
})); }));
it("should create resource", inject(function($browser) { it("should create resource", inject(function($httpBackend) {
$browser.xhr.expectPOST('/CreditCard', {name:'misko'}).respond({id:123, name:'misko'}); $httpBackend.expect('POST', '/CreditCard', '{"name":"misko"}').respond({id: 123, name: 'misko'});
var cc = CreditCard.save({name:'misko'}, callback); var cc = CreditCard.save({name: 'misko'}, callback);
nakedExpect(cc).toEqual({name:'misko'}); nakedExpect(cc).toEqual({name: 'misko'});
expect(callback).not.toHaveBeenCalled(); expect(callback).not.toHaveBeenCalled();
$browser.xhr.flush();
nakedExpect(cc).toEqual({id:123, name:'misko'}); $httpBackend.flush();
nakedExpect(cc).toEqual({id: 123, name: 'misko'});
expect(callback).toHaveBeenCalledWith(cc); expect(callback).toHaveBeenCalledWith(cc);
})); }));
it("should read resource", inject(function($browser) { it("should read resource", inject(function($httpBackend) {
$browser.xhr.expectGET("/CreditCard/123").respond({id:123, number:'9876'}); $httpBackend.expect('GET', '/CreditCard/123').respond({id: 123, number: '9876'});
var cc = CreditCard.get({id:123}, callback); var cc = CreditCard.get({id: 123}, callback);
expect(cc instanceof CreditCard).toBeTruthy(); expect(cc instanceof CreditCard).toBeTruthy();
nakedExpect(cc).toEqual({}); nakedExpect(cc).toEqual({});
expect(callback).not.toHaveBeenCalled(); expect(callback).not.toHaveBeenCalled();
$browser.xhr.flush();
nakedExpect(cc).toEqual({id:123, number:'9876'}); $httpBackend.flush();
nakedExpect(cc).toEqual({id: 123, number: '9876'});
expect(callback).toHaveBeenCalledWith(cc); expect(callback).toHaveBeenCalledWith(cc);
})); }));
it("should read partial resource", inject(function($browser) { it("should read partial resource", inject(function($httpBackend) {
$browser.xhr.expectGET("/CreditCard").respond([{id:{key:123}}]); $httpBackend.expect('GET', '/CreditCard').respond([{id:{key:123}}]);
$browser.xhr.expectGET("/CreditCard/123").respond({id:{key:123}, number:'9876'}); $httpBackend.expect('GET', '/CreditCard/123').respond({id: {key: 123}, number: '9876'});
var ccs = CreditCard.query(); var ccs = CreditCard.query();
$browser.xhr.flush();
$httpBackend.flush();
expect(ccs.length).toEqual(1); expect(ccs.length).toEqual(1);
var cc = ccs[0]; var cc = ccs[0];
expect(cc instanceof CreditCard).toBeTruthy(); expect(cc instanceof CreditCard).toBe(true);
expect(cc.number).not.toBeDefined(); expect(cc.number).toBeUndefined();
cc.$get(callback); cc.$get(callback);
$browser.xhr.flush(); $httpBackend.flush();
expect(callback).toHaveBeenCalledWith(cc); expect(callback).toHaveBeenCalledWith(cc);
expect(cc.number).toEqual('9876'); expect(cc.number).toEqual('9876');
})); }));
it("should update resource", inject(function($browser) { it("should update resource", inject(function($httpBackend) {
$browser.xhr.expectPOST('/CreditCard/123', {id:{key:123}, name:'misko'}).respond({id:{key:123}, name:'rama'}); $httpBackend.expect('POST', '/CreditCard/123', '{"id":{"key":123},"name":"misko"}').
respond({id: {key: 123}, name: 'rama'});
var cc = CreditCard.save({id:{key:123}, name:'misko'}, callback); var cc = CreditCard.save({id: {key: 123}, name: 'misko'}, callback);
nakedExpect(cc).toEqual({id:{key:123}, name:'misko'}); nakedExpect(cc).toEqual({id:{key:123}, name:'misko'});
expect(callback).not.toHaveBeenCalled(); expect(callback).not.toHaveBeenCalled();
$browser.xhr.flush(); $httpBackend.flush();
})); }));
it("should query resource", inject(function($browser) { it("should query resource", inject(function($httpBackend) {
$browser.xhr.expectGET("/CreditCard?key=value").respond([{id:1}, {id:2}]); $httpBackend.expect('GET', '/CreditCard?key=value').respond([{id: 1}, {id: 2}]);
var ccs = CreditCard.query({key:'value'}, callback); var ccs = CreditCard.query({key: 'value'}, callback);
expect(ccs).toEqual([]); expect(ccs).toEqual([]);
expect(callback).not.toHaveBeenCalled(); expect(callback).not.toHaveBeenCalled();
$browser.xhr.flush();
$httpBackend.flush();
nakedExpect(ccs).toEqual([{id:1}, {id:2}]); nakedExpect(ccs).toEqual([{id:1}, {id:2}]);
expect(callback).toHaveBeenCalledWith(ccs); expect(callback).toHaveBeenCalledWith(ccs);
})); }));
it("should have all arguments optional", inject(function($browser) { it("should have all arguments optional", inject(function($httpBackend) {
$browser.xhr.expectGET('/CreditCard').respond([{id:1}]); $httpBackend.expect('GET', '/CreditCard').respond([{id:1}]);
var log = ''; var log = '';
var ccs = CreditCard.query(function() { log += 'cb;'; }); var ccs = CreditCard.query(function() { log += 'cb;'; });
$browser.xhr.flush();
$httpBackend.flush();
nakedExpect(ccs).toEqual([{id:1}]); nakedExpect(ccs).toEqual([{id:1}]);
expect(log).toEqual('cb;'); expect(log).toEqual('cb;');
})); }));
it('should delete resource and call callback', inject(function($browser) { it('should delete resource and call callback', inject(function($httpBackend) {
$browser.xhr.expectDELETE("/CreditCard/123").respond(200, {}); $httpBackend.expect('DELETE', '/CreditCard/123').respond({});
$httpBackend.expect('DELETE', '/CreditCard/333').respond(204, null);
CreditCard.remove({id:123}, callback); CreditCard.remove({id:123}, callback);
expect(callback).not.toHaveBeenCalled(); expect(callback).not.toHaveBeenCalled();
$browser.xhr.flush();
$httpBackend.flush();
nakedExpect(callback.mostRecentCall.args).toEqual([{}]); nakedExpect(callback.mostRecentCall.args).toEqual([{}]);
callback.reset(); callback.reset();
$browser.xhr.expectDELETE("/CreditCard/333").respond(204, null);
CreditCard.remove({id:333}, callback); CreditCard.remove({id:333}, callback);
expect(callback).not.toHaveBeenCalled(); expect(callback).not.toHaveBeenCalled();
$browser.xhr.flush();
$httpBackend.flush();
nakedExpect(callback.mostRecentCall.args).toEqual([{}]); nakedExpect(callback.mostRecentCall.args).toEqual([{}]);
})); }));
it('should post charge verb', inject(function($browser) { it('should post charge verb', inject(function($httpBackend) {
$browser.xhr.expectPOST('/CreditCard/123!charge?amount=10', {auth:'abc'}).respond({success:'ok'}); $httpBackend.expect('POST', '/CreditCard/123!charge?amount=10', '{"auth":"abc"}').respond({success: 'ok'});
CreditCard.charge({id:123, amount:10}, {auth:'abc'}, callback);
CreditCard.charge({id:123, amount:10},{auth:'abc'}, callback);
})); }));
it('should post charge verb on instance', inject(function($browser) { it('should post charge verb on instance', inject(function($httpBackend) {
$browser.xhr.expectPOST('/CreditCard/123!charge?amount=10', {id:{key:123}, name:'misko'}).respond({success:'ok'}); $httpBackend.expect('POST', '/CreditCard/123!charge?amount=10',
'{"id":{"key":123},"name":"misko"}').respond({success: 'ok'});
var card = new CreditCard({id:{key:123}, name:'misko'}); var card = new CreditCard({id:{key:123}, name:'misko'});
card.$charge({amount:10}, callback); card.$charge({amount:10}, callback);
})); }));
it('should create on save', inject(function($browser) { it('should create on save', inject(function($httpBackend) {
$browser.xhr.expectPOST('/CreditCard', {name:'misko'}).respond({id:123}); $httpBackend.expect('POST', '/CreditCard', '{"name":"misko"}').respond({id: 123});
var cc = new CreditCard(); var cc = new CreditCard();
expect(cc.$get).toBeDefined(); expect(cc.$get).toBeDefined();
expect(cc.$query).toBeDefined(); expect(cc.$query).toBeDefined();
@ -198,97 +218,72 @@ xdescribe("resource", function() {
cc.name = 'misko'; cc.name = 'misko';
cc.$save(callback); cc.$save(callback);
nakedExpect(cc).toEqual({name:'misko'}); nakedExpect(cc).toEqual({name:'misko'});
$browser.xhr.flush();
$httpBackend.flush();
nakedExpect(cc).toEqual({id:123}); nakedExpect(cc).toEqual({id:123});
expect(callback).toHaveBeenCalledWith(cc); expect(callback).toHaveBeenCalledWith(cc);
})); }));
it('should not mutate the resource object if response contains no body', inject(function($browser) { it('should not mutate the resource object if response contains no body', inject(function($httpBackend) {
var data = {id:{key:123}, number:'9876'}; var data = {id:{key:123}, number:'9876'};
$browser.xhr.expectGET("/CreditCard/123").respond(data); $httpBackend.expect('GET', '/CreditCard/123').respond(data);
var cc = CreditCard.get({id:123}); $httpBackend.expect('POST', '/CreditCard/123', toJson(data)).respond('');
$browser.xhr.flush();
expect(cc instanceof CreditCard).toBeTruthy();
var idBefore = cc.id;
$browser.xhr.expectPOST("/CreditCard/123", data).respond(''); var cc = CreditCard.get({id:123});
$httpBackend.flush();
expect(cc instanceof CreditCard).toBe(true);
var idBefore = cc.id;
cc.$save(); cc.$save();
$browser.xhr.flush(); $httpBackend.flush();
expect(idBefore).toEqual(cc.id); expect(idBefore).toEqual(cc.id);
})); }));
it('should bind default parameters', inject(function($browser) { it('should bind default parameters', inject(function($httpBackend) {
$browser.xhr.expectGET('/CreditCard/123.visa?minimum=0.05').respond({id:123}); $httpBackend.expect('GET', '/CreditCard/123.visa?minimum=0.05').respond({id: 123});
var Visa = CreditCard.bind({verb:'.visa', minimum:0.05}); var Visa = CreditCard.bind({verb:'.visa', minimum:0.05});
var visa = Visa.get({id:123}); var visa = Visa.get({id:123});
$browser.xhr.flush(); $httpBackend.flush();
nakedExpect(visa).toEqual({id:123}); nakedExpect(visa).toEqual({id:123});
})); }));
it('should excersize full stack', inject(function($rootScope, $browser, $resource, $compile) { it('should excersize full stack', inject(function($httpBackend, $resource) {
$compile('<div></div>')($rootScope);
var Person = $resource('/Person/:id'); var Person = $resource('/Person/:id');
$browser.xhr.expectGET('/Person/123').respond('\n{\n"name":\n"misko"\n}\n');
$httpBackend.expect('GET', '/Person/123').respond('\n{\n"name":\n"misko"\n}\n');
var person = Person.get({id:123}); var person = Person.get({id:123});
$browser.xhr.flush(); $httpBackend.flush();
expect(person.name).toEqual('misko'); expect(person.name).toEqual('misko');
})); }));
it('should return the same object when verifying the cache',
inject(function($rootScope, $compile, $browser, $resource) {
$compile('<div></div>')($rootScope);
var Person = $resource('/Person/:id', null, {query: {method:'GET', isArray: true, verifyCache: true}});
$browser.xhr.expectGET('/Person/123').respond('[\n{\n"name":\n"misko"\n}\n]');
var person = Person.query({id:123});
$browser.xhr.flush();
expect(person[0].name).toEqual('misko');
$browser.xhr.expectGET('/Person/123').respond('[\n{\n"name":\n"rob"\n}\n]');
var person2 = Person.query({id:123});
$browser.defer.flush();
expect(person2[0].name).toEqual('misko');
var person2Cache = person2;
$browser.xhr.flush();
expect(person2Cache).toEqual(person2);
expect(person2[0].name).toEqual('rob');
}));
describe('failure mode', function() { describe('failure mode', function() {
var ERROR_CODE = 500, var ERROR_CODE = 500,
ERROR_RESPONSE = 'Server Error', ERROR_RESPONSE = 'Server Error',
errorCB; errorCB;
beforeEach(function() { beforeEach(function() {
errorCB = jasmine.createSpy(); errorCB = jasmine.createSpy('error').andCallFake(function(response, status) {
expect(response).toBe(ERROR_RESPONSE);
expect(status).toBe(ERROR_CODE);
});
}); });
it('should report error when non 2xx if error callback is not provided', it('should call the error callback if provided on non 2xx response', inject(function($httpBackend) {
inject(function($browser, $xhrError) { $httpBackend.expect('GET', '/CreditCard/123').respond(ERROR_CODE, ERROR_RESPONSE);
$browser.xhr.expectGET('/CreditCard/123').respond(ERROR_CODE, ERROR_RESPONSE);
CreditCard.get({id:123});
$browser.xhr.flush();
expect($xhrError).toHaveBeenCalled();
}));
it('should call the error callback if provided on non 2xx response',
inject(function($browser, $xhrError) {
$browser.xhr.expectGET('/CreditCard/123').respond(ERROR_CODE, ERROR_RESPONSE);
CreditCard.get({id:123}, callback, errorCB); CreditCard.get({id:123}, callback, errorCB);
$browser.xhr.flush(); $httpBackend.flush();
expect(errorCB).toHaveBeenCalledWith(500, ERROR_RESPONSE); expect(errorCB).toHaveBeenCalledOnce();
expect(callback).not.toHaveBeenCalled(); expect(callback).not.toHaveBeenCalled();
expect($xhrError).not.toHaveBeenCalled();
})); }));
it('should call the error callback if provided on non 2xx response', it('should call the error callback if provided on non 2xx response', inject(function($httpBackend) {
inject(function($browser, $xhrError) { $httpBackend.expect('GET', '/CreditCard').respond(ERROR_CODE, ERROR_RESPONSE);
$browser.xhr.expectGET('/CreditCard').respond(ERROR_CODE, ERROR_RESPONSE);
CreditCard.get(callback, errorCB); CreditCard.get(callback, errorCB);
$browser.xhr.flush(); $httpBackend.flush();
expect(errorCB).toHaveBeenCalledWith(500, ERROR_RESPONSE); expect(errorCB).toHaveBeenCalledOnce();
expect(callback).not.toHaveBeenCalled(); expect(callback).not.toHaveBeenCalled();
expect($xhrError).not.toHaveBeenCalled();
})); }));
}); });
}); });