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

fix(ngMockE2E): ensure that mocked $httpBackend uses correct $browser #15601

Merged
merged 1 commit into from
Jan 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions src/ngMock/angular-mocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -1313,9 +1313,8 @@ angular.mock.dump = function(object) {
});
```
*/
angular.mock.$HttpBackendProvider = function() {
this.$get = ['$rootScope', '$timeout', createHttpBackendMock];
};
angular.mock.$httpBackendDecorator =
['$rootScope', '$timeout', '$delegate', createHttpBackendMock];

/**
* General factory function for $httpBackend mock.
Expand All @@ -1336,7 +1335,10 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
expectations = [],
responses = [],
responsesPush = angular.bind(responses, responses.push),
copy = angular.copy;
copy = angular.copy,
// We cache the original backend so that if both ngMock and ngMockE2E override the
// service the ngMockE2E version can pass through to the real backend
originalHttpBackend = $delegate.$$originalHttpBackend || $delegate;

function createResponse(status, data, headers, statusText) {
if (angular.isFunction(status)) return status;
Expand Down Expand Up @@ -1421,7 +1423,7 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
// if $browser specified, we do auto flush all requests
($browser ? $browser.defer : responsesPush)(wrapResponse(definition));
} else if (definition.passThrough) {
$delegate(method, url, data, callback, headers, timeout, withCredentials, responseType, eventHandlers, uploadEventHandlers);
originalHttpBackend(method, url, data, callback, headers, timeout, withCredentials, responseType, eventHandlers, uploadEventHandlers);
} else throw new Error('No response defined !');
return;
}
Expand Down Expand Up @@ -1897,6 +1899,8 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
responses.length = 0;
};

$httpBackend.$$originalHttpBackend = originalHttpBackend;

return $httpBackend;


Expand Down Expand Up @@ -2394,14 +2398,14 @@ angular.module('ngMock', ['ng']).provider({
$exceptionHandler: angular.mock.$ExceptionHandlerProvider,
$log: angular.mock.$LogProvider,
$interval: angular.mock.$IntervalProvider,
$httpBackend: angular.mock.$HttpBackendProvider,
$rootElement: angular.mock.$RootElementProvider,
$componentController: angular.mock.$ComponentControllerProvider
}).config(['$provide', '$compileProvider', function($provide, $compileProvider) {
$provide.decorator('$timeout', angular.mock.$TimeoutDecorator);
$provide.decorator('$$rAF', angular.mock.$RAFDecorator);
$provide.decorator('$rootScope', angular.mock.$RootScopeDecorator);
$provide.decorator('$controller', createControllerDecorator($compileProvider));
$provide.decorator('$httpBackend', angular.mock.$httpBackendDecorator);
}]);

/**
Expand All @@ -2416,7 +2420,6 @@ angular.module('ngMock', ['ng']).provider({
* the {@link ngMockE2E.$httpBackend e2e $httpBackend} mock.
*/
angular.module('ngMockE2E', ['ng']).config(['$provide', function($provide) {
$provide.value('$httpBackend', angular.injector(['ng']).get('$httpBackend'));
$provide.decorator('$httpBackend', angular.mock.e2e.$httpBackendDecorator);
}]);

Expand Down
14 changes: 12 additions & 2 deletions test/ngMock/angular-mocksSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2432,13 +2432,15 @@ describe('ngMock', function() {

describe('ngMockE2E', function() {
describe('$httpBackend', function() {
var hb, realHttpBackend, callback;
var hb, realHttpBackend, realHttpBackendBrowser, callback;

beforeEach(function() {
callback = jasmine.createSpy('callback');
angular.module('ng').config(function($provide) {
realHttpBackend = jasmine.createSpy('real $httpBackend');
$provide.value('$httpBackend', realHttpBackend);
$provide.factory('$httpBackend', ['$browser', function($browser) {
return realHttpBackend.and.callFake(function() { realHttpBackendBrowser = $browser; });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't the following work as well (just trying to understand if I am missing something):

$provide.factory('$httpBackend', ['$browser', function($browser) {
  realHttpBackendBrowser = $browser;
  return realHttpBackend;
}]);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This way we would be able to be sure that the factory had been called but not that the correct httpBackend service had been called, no?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aren't you explicitly checking about the correct service anyway?
(I am not saying you should change it, just wondering.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we check that the spy has been called so I guess you are right.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But since I have a LGTM from you and Travis is green I am going to merge it as-is :-P

}]);
});
module('ngMockE2E');
inject(function($injector) {
Expand Down Expand Up @@ -2477,6 +2479,14 @@ describe('ngMockE2E', function() {
expect(realHttpBackend).not.toHaveBeenCalled();
expect(callback).toHaveBeenCalledOnceWith(200, 'passThrough override', '', '');
}));

it('should pass through to an httpBackend that uses the same $browser service', inject(function($browser) {
hb.when('GET', /\/passThrough\/.*/).passThrough();
hb('GET', '/passThrough/23');

expect(realHttpBackend).toHaveBeenCalledOnce();
expect(realHttpBackendBrowser).toBe($browser);
}));
});


Expand Down