Jasmine clock
For cases, when we need to use the setTimeout()
or setInterval()
functions of JavaScript in our tests: Jasmine provides a clock that can make use of these functions available.
jasmine.clock().install()
installs a clock for our specs and jasmine.clock().uninstall()
restores the original JavaScript timer functions. Jasmine clock helps us handle time-dependent code using these functions. Let's see the following examples to understand how these work.
Suppose we want to check if Alice, an employee, was available after an hour, we create a spy for the checkAvailabity()
function for an employee
object:
var employee; beforeEach(function() { employee = new Employee("Alice", 5, "Testing"); employee.checkAvailability = jasmine.createSpy("checkAvailability"); jasmine.clock().install(); }); afterEach(function() { jasmine.clock().uninstall(); });
We installed clock in beforeEach()
and uninstalled it in afterEach()
so that the original JavaScript timer functions are...