diff --git a/src/ng/testability.js b/src/ng/testability.js index c9d0afd2c38e..202159de389b 100644 --- a/src/ng/testability.js +++ b/src/ng/testability.js @@ -109,7 +109,11 @@ function $$TestabilityProvider() { * @param {function} callback */ testability.whenStable = function(callback) { - $browser.notifyWhenNoOutstandingRequests(callback); + // Register the callback asynchronously, so that services that trigger + // async callbacks have a chance to run. + $rootScope.$evalAsync(function() { + $browser.notifyWhenNoOutstandingRequests(callback); + }); }; return testability; diff --git a/test/ng/testabilitySpec.js b/test/ng/testabilitySpec.js index ce40de5f4966..7e27fb52156a 100644 --- a/test/ng/testabilitySpec.js +++ b/test/ng/testabilitySpec.js @@ -188,11 +188,36 @@ describe('$$testability', function() { }); describe('waiting for stability', function() { - it('should process callbacks immediately with no outstanding requests', - inject(function($$testability) { + it('should process callbacks without waiting if there are no outstanding requests', + inject(function($$testability, $timeout) { var callback = jasmine.createSpy('callback'); - $$testability.whenStable(callback); - expect(callback).toHaveBeenCalled(); + runs(function() { + $$testability.whenStable(callback); + $timeout.flush(); + }); + + waitsFor(function() { + return callback.calls.length > 0; + }, "The callback should be called.", 500); + })); + + it('should wait for new $http calls asynchronously', + inject(function($$testability, $httpBackend, $http, $timeout) { + var callback = jasmine.createSpy('callback'); + runs(function() { + $httpBackend.when('GET').respond(200); + + $http.get(''); + $$testability.whenStable(callback); + expect(callback).not.toHaveBeenCalled(); + + $httpBackend.flush(); + $timeout.flush(); + }); + + waitsFor(function() { + return callback.calls.length > 0; + }, "The callback should be called.", 500); })); }); });