Created
September 14, 2016 05:29
-
-
Save VivienAdnot/dc265a9c17dfa4f9d04a17c1a561e441 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
it('should simulate promise', inject(function($q, $rootScope) { | |
var deferred = $q.defer(); | |
var promise = deferred.promise; | |
var resolvedValue; | |
promise.then(function(value) { resolvedValue = value; }); | |
expect(resolvedValue).toBeUndefined(); | |
// Simulate resolving of promise | |
deferred.resolve(123); | |
// Note that the 'then' function does not get called synchronously. | |
// This is because we want the promise API to always be async, whether or not | |
// it got called synchronously or asynchronously. | |
expect(resolvedValue).toBeUndefined(); | |
// Propagate promise resolution to 'then' functions using $apply(). | |
$rootScope.$apply(); | |
expect(resolvedValue).toEqual(123); | |
})); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment