//--- CODE --------------------------
angular.module('myApp', []).service('MyService', ['$q', '$timeout', function ($q, $timeout) {
this.foo = function () {
var deferred = $q.defer();
$timeout(function () {
deferred.resolve('resolved value');
}, 90000);
return deferred.promise;
}
}]);
// --- SPECS -------------------------
describe('$timeout', function () {
beforeEach(function () {
// Register module configuation code for myApp
module('myApp');
});
it("flushes timeout synchronously", inject(function (MyService, $timeout) {
var valueToVerify,
resolvedSpy = jasmine.createSpy();
// Call service under test with mock object
MyService.foo().then(resolvedSpy);
expect(resolvedSpy).not.toHaveBeenCalled();
// Timeout immediately executes function on flush()
$timeout.flush();
expect(resolvedSpy).toHaveBeenCalledWith('resolved value');
}));
it('should throw an exception when not flushed', inject(function ($timeout) {
$timeout(angular.noop);
expect(function () {
$timeout.verifyNoPendingTasks();
}).toThrow();
}));
it('should do nothing when all tasks have been flushed', inject(function ($timeout) {
$timeout(angular.noop);
$timeout.flush();
expect(function () {
$timeout.verifyNoPendingTasks();
}).not.toThrow();
}));
});
describe('$exceptionHandlerProvider and $LogProvider', function () {
it('should capture log messages and exceptions', function () {
// Mock implementation of $log gathers all logged messages in arrays
// These arrays are exposed as `logs` property of each of the
// level-specific log function
module(function ($exceptionHandlerProvider) {
// "log" mode stores an array of errors in $exceptionHandler.errors
$exceptionHandlerProvider.mode('log');
...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.