Interestings
- Always call jasmine.Clock.useMock() after anything that uses setTimeout or setInterval (e.g. _.throttle)
This is smallest example that reproduces the problem we saw in our codebase:
describe(”underscore”, function() {
var foo, throttledFoo;
beforeEach(function() {
jasmine.Clock.useMock(); // bad!
var Mod = Backbone.RelationalModel.extend({});
var Col = Backbone.Collection.extend({model: Mod});
new Col([{selected: false, trashed: false}]);
foo = jasmine.createSpy();
throttledFoo = _.throttle(foo, 500);
// jasmine.Clock.useMock(); // good!
});
it(”should 1″, function() {
throttledFoo();
});
it(”should 2″, function() {
throttledFoo();
});
it(”should 3″, function() {
});
it(”should 4″, function() {
});
it(”should consistently pass but doesn’t”, function() {
expect(foo.calls.length).toEqual(0);
throttledFoo();
expect(foo.calls.length).toEqual(1);
throttledFoo();
expect(foo.calls.length).toEqual(1);
jasmine.Clock.tick(501);
expect(foo.calls.length).toEqual(2);
});
});