javascript - Trying to use Jasmine spies and getting "is not a function" errors -
here class:
class clock { // runs callback while continues // returns truthy values constructor(callback, delay) { this.callback = callback; this.delay = delay; this.timerid = null; } start() { this.timerid = settimeout(() => { if (this.callback.call()) this.start(); }, this.delay); } // want kill time? kill() { cleartimeout(this.timerid); } } export default clock;
and relevant tests:
import clock '../../src/js/lib/clock.js'; describe("clock", function() { it("has callback , delay", function() { let clock = clockfactory(); expect(clock.callback).tobedefined(); expect(clock.delay).tobedefined(); }); describe("is clock", function() { let observer, clock; beforeeach(function() { jasmine.clock().install(); observer = { callback: function() {} }; spyon(observer, 'callback') console.log(observer) clock = clockfactory({callback: observer}); }); aftereach(function() { jasmine.clock().uninstall(); clock.kill() }); it("it periodically executes callback", function() { clock.start() expect(observer.callback).not.tohavebeencalled(); jasmine.clock().tick(5001); expect(observer.callback.calls.count()).toequal(1); jasmine.clock().tick(2500); expect(observer.callback.calls.count()).toequal(1); jasmine.clock().tick(2500); expect(observer.callback.calls.count()).toequal(2); }); it("it can stop ticking", function() { clock.start() expect(observer.callback).not.tohavebeencalled(); jasmine.clock().tick(5001); expect(observer.callback.calls.count()).toequal(1); clock.kill() jasmine.clock().tick(5000); expect(observer.callback.calls.count()).toequal(1); }); }); function clockfactory(options = {}) { return new clock( (options.callback || function() {}), options.delay || 5000 ); } });
now when run tests, following failures:
failures: 1) clock clock periodically executes callback message: typeerror: _this.callback.call not function stack: typeerror: _this.callback.call not function @ clock.js:14:25 @ object.<anonymous> (clock_spec.js:34:23) 2) clock clock can stop ticking message: typeerror: _this.callback.call not function stack: typeerror: _this.callback.call not function @ clock.js:14:25 @ object.<anonymous> (clock_spec.js:48:23)
i think i'm using spies correctly based on examples, evidently there problem! suggestions?
your clock
constructor expects function (or @ least object call()
method) first argument. in tests, invoking clockfactory
like
observer = { callback: function () {} }; spyon(observer, 'callback'); clock = clockfactory({ callback: observer });
i.e. first argument being passed clockfactory
is
{ callback: { callback: function () {} } }
this not intended (unless clockfactory
signature not match clock
constructor signature). try doing like
observer = jasmine.createspy('observer'); clock = clockfactory(observer);
alternatively, can use "options object" pattern in clock
constructor , write like
constructor({callback, delay}) { this.callback = callback; this.delay = delay; ... }
then, can pass single object callback
, delay
properties, like
observer = { callback: function () {} }; spyon(observer, 'callback'); clock = clockfactory(observer);
Comments
Post a Comment