Javascript 为什么我会收到预期错误?
我已经为我的库创建了一个规范,以确保以固定的间隔发出一个值。我正在使用sinonjs,并作为间谍创建了一个回调。我使用sinonjs中的假计时器来模拟额外的两个10秒间隔。但是,当测试在首次使用tick方法时着陆,这是发出错误的时候。我产生下面的错误Javascript 为什么我会收到预期错误?,javascript,unit-testing,mocha.js,sinon,sinon-chai,Javascript,Unit Testing,Mocha.js,Sinon,Sinon Chai,我已经为我的库创建了一个规范,以确保以固定的间隔发出一个值。我正在使用sinonjs,并作为间谍创建了一个回调。我使用sinonjs中的假计时器来模拟额外的两个10秒间隔。但是,当测试在首次使用tick方法时着陆,这是发出错误的时候。我产生下面的错误 ExpectationError: Unexpected call: getTheValue() Expectation met: getTheValue([...]) once 下面是我的测试代码 it('should emit the val
ExpectationError: Unexpected call: getTheValue()
Expectation met: getTheValue([...]) once
下面是我的测试代码
it('should emit the values at an interval', function () {
var callback = this.sandbox.spy();
var interval = this.sandbox.useFakeTimers();
this.myLib.emitValues(callback);
interval.tick(1000);
interval.tick(1000);
callback.should.have.been.calledWith('test');
});
这是我的生产代码
_getValue() {
var value = getTheValue(this.id);
this.myListener(value);
}
emitValues(callback) {
this.myListener = callback;
this._getValue();
setInterval(() => this._getValue(), 1000);
}
有人知道我为什么会出现此错误吗?如果希望多次调用回调,请使用
calledWith
或calledWith
,并为每个预期调用提供各种参数
只是Sinon之上的API包装器。因此,您可以阅读更多关于这些期望的信息。向我们展示生产代码。我已经添加了生产代码。我仍然认为我们没有完全看到您的代码。例如,在何处定义
getTheValue
?假货在哪里?这个用了很多。向我们展示随附的代码,以便我们能够找出您的外部范围。用于测试和生产代码。