From a644ca7b4e6ba84a467bcabed8f99386eda7fb14 Mon Sep 17 00:00:00 2001 From: Pete Bacon Darwin Date: Tue, 30 Jul 2013 22:48:56 +0100 Subject: fix(resource): check whether response matches action.isArray When using $resource you must setup your actions carefully based on what the server returns. If the server responds to a request with an array then you must configure the action with `isArray:true` and vice versa. The built-in `get` action defaults to `isArray:false` and the `query` action defaults to `isArray:true`, which is must be changed if the server does not do this. Before the error message was an exception inside angular.copy, which didn't explain what the real problem was. Rather than changing the way that angular.copy works, this change ensures that a better error message is provided to the programmer if they do not set up their resource actions correctly. Closes #2255, #1044 --- test/ngResource/resourceSpec.js | 49 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) (limited to 'test') diff --git a/test/ngResource/resourceSpec.js b/test/ngResource/resourceSpec.js index 52395297..19456f01 100644 --- a/test/ngResource/resourceSpec.js +++ b/test/ngResource/resourceSpec.js @@ -807,7 +807,6 @@ describe("resource", function() { }); }); - it('should transform request/response', function() { var Person = $resource('/Person/:id', {}, { save: { @@ -1034,3 +1033,51 @@ describe("resource", function() { }); }); }); + +describe('resource', function() { + var $httpBackend, $resource; + + beforeEach(module(function($exceptionHandlerProvider) { + $exceptionHandlerProvider.mode('log'); + })); + + beforeEach(module('ngResource')); + + beforeEach(inject(function($injector) { + $httpBackend = $injector.get('$httpBackend'); + $resource = $injector.get('$resource'); + })); + + + it('should fail if action expects an object but response is an array', function() { + var successSpy = jasmine.createSpy('successSpy'); + var failureSpy = jasmine.createSpy('failureSpy'); + + $httpBackend.expect('GET', '/Customer/123').respond({id: 'abc'}); + + $resource('/Customer/123').query() + .$promise.then(successSpy, function(e) { failureSpy(e.message); }); + $httpBackend.flush(); + + expect(successSpy).not.toHaveBeenCalled(); + expect(failureSpy).toHaveBeenCalledWith( + '[ngResource:badcfg] Error in resource configuration. Expected response to contain an array but got an object'); + }); + + it('should fail if action expects an array but response is an object', function() { + var successSpy = jasmine.createSpy('successSpy'); + var failureSpy = jasmine.createSpy('failureSpy'); + + $httpBackend.expect('GET', '/Customer/123').respond([1,2,3]); + + $resource('/Customer/123').get() + .$promise.then(successSpy, function(e) { failureSpy(e.message); }); + $httpBackend.flush(); + + expect(successSpy).not.toHaveBeenCalled(); + expect(failureSpy).toHaveBeenCalledWith( + '[ngResource:badcfg] Error in resource configuration. Expected response to contain an object but got an array'); + }); + + +}); \ No newline at end of file -- cgit v1.2.3