Skip to content

Instantly share code, notes, and snippets.

@VivienAdnot
Created September 14, 2016 05:29
Show Gist options
  • Save VivienAdnot/dc265a9c17dfa4f9d04a17c1a561e441 to your computer and use it in GitHub Desktop.
Save VivienAdnot/dc265a9c17dfa4f9d04a17c1a561e441 to your computer and use it in GitHub Desktop.
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