Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit f69dc16

Browse files
kseamonbtford
authored andcommittedDec 11, 2013
fix(angular-mocks): use copy of mock data in $httpBackend
Copy mock data returned from the mock $httpBackend. This prevents modifications to the response from affecting future responses. Previously, this misbehavior was being mitigated by the deep copy in $resource, but that no longer exists.
1 parent f1a8d41 commit f69dc16

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed
 

‎src/ngMock/angular-mocks.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1087,7 +1087,8 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
10871087
var definitions = [],
10881088
expectations = [],
10891089
responses = [],
1090-
responsesPush = angular.bind(responses, responses.push);
1090+
responsesPush = angular.bind(responses, responses.push),
1091+
copy = angular.copy;
10911092

10921093
function createResponse(status, data, headers) {
10931094
if (angular.isFunction(status)) return status;
@@ -1119,7 +1120,7 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
11191120
function handleResponse() {
11201121
var response = wrapped.response(method, url, data, headers);
11211122
xhr.$$respHeaders = response[2];
1122-
callback(response[0], response[1], xhr.getAllResponseHeaders());
1123+
callback(copy(response[0]), copy(response[1]), xhr.getAllResponseHeaders());
11231124
}
11241125

11251126
function handleTimeout() {

‎test/ngMock/angular-mocksSpec.js

+28-2
Original file line numberDiff line numberDiff line change
@@ -766,7 +766,7 @@ describe('ngMock', function() {
766766

767767
describe('object literal format', function() {
768768
var mock = { log: 'module' };
769-
769+
770770
beforeEach(function() {
771771
module({
772772
'service': mock,
@@ -782,7 +782,7 @@ describe('ngMock', function() {
782782
expect(service).toEqual(mock);
783783
});
784784
});
785-
785+
786786
it('should support multiple key value pairs', function() {
787787
inject(function(service, other) {
788788
expect(other.some).toEqual('replacement');
@@ -891,6 +891,32 @@ describe('ngMock', function() {
891891
});
892892

893893

894+
it('should respond with a copy of the mock data', function() {
895+
var mockObject = {a: 'b'};
896+
897+
hb.when('GET', '/url1').respond(200, mockObject, {});
898+
899+
callback.andCallFake(function(status, response) {
900+
expect(status).toBe(200);
901+
expect(response).toEqual({a: 'b'});
902+
expect(response).not.toBe(mockObject);
903+
response.a = 'c';
904+
});
905+
906+
hb('GET', '/url1', null, callback);
907+
hb.flush();
908+
expect(callback).toHaveBeenCalledOnce();
909+
910+
// Fire it again and verify that the returned mock data has not been
911+
// modified.
912+
callback.reset();
913+
hb('GET', '/url1', null, callback);
914+
hb.flush();
915+
expect(callback).toHaveBeenCalledOnce();
916+
expect(mockObject).toEqual({a: 'b'});
917+
});
918+
919+
894920
it('should throw error when unexpected request', function() {
895921
hb.when('GET', '/url1').respond(200, 'content');
896922
expect(function() {

0 commit comments

Comments
 (0)
This repository has been archived.