gpt4 book ai didi

javascript - 尝试使用 Jasmine spy 并收到 "is not a function"错误

转载 作者:行者123 更新时间:2023-12-03 07:55:23 25 4
gpt4 key购买 nike

这是我的类(class):

class Clock {

// Runs callback while it continues
// to 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 to kill some time?

kill() {
clearTimeout(this.timerId);
}

}

export default Clock;

以及相关测试:

import Clock from '../../src/js/lib/clock.js';

describe("Clock", function() {

it("Has a callback and a delay", function() {
let clock = ClockFactory();
expect(clock.callback).toBeDefined();
expect(clock.delay).toBeDefined();
});

describe("Is a 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 a 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
);
}

});

现在,当我运行测试时,我遇到以下失败:

Failures:
1) Clock Is a clock It periodically executes a callback
Message:
TypeError: _this.callback.call is not a function
Stack:
TypeError: _this.callback.call is not a function
at clock.js:14:25
at Object.<anonymous> (clock_spec.js:34:23)

2) Clock Is a clock It can stop ticking
Message:
TypeError: _this.callback.call is not a function
Stack:
TypeError: _this.callback.call is not a function
at clock.js:14:25
at Object.<anonymous> (clock_spec.js:48:23)

我认为我根据示例正确使用了 spy ,但显然存在问题!有什么建议吗?

最佳答案

您的Clock 构造函数需要一个函数(或至少一个具有call() 方法的对象)作为其第一个参数。在您的测试中,您调用 ClockFactory 就像

observer = { callback: function () {} };
spyOn(observer, 'callback');
clock = ClockFactory({ callback: observer });

即传递给 ClockFactory 的第一个参数是

{ callback: { callback: function () {} } }

这很可能不是您想要的(除非您的 ClockFactory 签名与您的 Clock 构造函数签名不匹配)。尝试做类似的事情

observer = jasmine.createSpy('observer');
clock = ClockFactory(observer);
<小时/>

或者,您可以在 Clock 构造函数中使用“选项对象”模式并编写类似的内容

constructor({callback, delay}) {
this.callback = callback;
this.delay = delay;
...
}

然后,您可以传递具有 callbackdelay 属性的单个对象,例如

observer = { callback: function () {} };
spyOn(observer, 'callback');
clock = ClockFactory(observer);

关于javascript - 尝试使用 Jasmine spy 并收到 "is not a function"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34821279/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com