否则间谍 callCount returns 0
Sinon spy callCount returns 0
我似乎无法进行测试。我有一个像这样的简单混合:
export const mixin = superclass => class mixin extends superclass {
constructor() {
super();
this.addEventListener('do-it', this.doIt);
}
doIt() {
console.log('did it');
}
};
还有一个简单的测试:
describe('mixin', () => {
it('should call doIt', () => {
class TestElement extends mixin(HTMLElement) {}
customElements.define('test-element', TestElement);
const el = new TestElement();
const spy = sinon.spy(el, 'doIt');
el.dispatchEvent(new CustomEvent('do-it'));
expect(spy.callCount).to.equal(1);
});
});
https://jsfiddle.net/nbuLhvkd/
它记录 did it
但间谍的 callCount
值为 0
。如果我做const spy = sinon.spy(console, 'log');
,间谍的callCount
就是1
。监视实例方法的正确方法是什么?
很可能您的'dispatchEvent'调用是异步的,因此callCount确实是0,因为它是同步执行的。
否则你的语法很好 - 正如你在控制台调用上的测试所证明的那样。
我使用了TestElement.prototype
进行侦察,并且在实例化new TestElement();
之前也移动了它。它现在有效,但有人可以解释为什么吗?
describe('Sinon examples', () => {
it('logs test on do-it', () => {
class TestElement extends mixin(HTMLElement) {}
customElements.define('test-element', TestElement);
const spy = sinon.spy(TestElement.prototype, 'doIt');
const el = new TestElement();
el.dispatchEvent(new CustomEvent('do-it'));
expect(spy.calledOnce).to.be.true;
});
});
我似乎无法进行测试。我有一个像这样的简单混合:
export const mixin = superclass => class mixin extends superclass {
constructor() {
super();
this.addEventListener('do-it', this.doIt);
}
doIt() {
console.log('did it');
}
};
还有一个简单的测试:
describe('mixin', () => {
it('should call doIt', () => {
class TestElement extends mixin(HTMLElement) {}
customElements.define('test-element', TestElement);
const el = new TestElement();
const spy = sinon.spy(el, 'doIt');
el.dispatchEvent(new CustomEvent('do-it'));
expect(spy.callCount).to.equal(1);
});
});
https://jsfiddle.net/nbuLhvkd/
它记录 did it
但间谍的 callCount
值为 0
。如果我做const spy = sinon.spy(console, 'log');
,间谍的callCount
就是1
。监视实例方法的正确方法是什么?
很可能您的'dispatchEvent'调用是异步的,因此callCount确实是0,因为它是同步执行的。 否则你的语法很好 - 正如你在控制台调用上的测试所证明的那样。
我使用了TestElement.prototype
进行侦察,并且在实例化new TestElement();
之前也移动了它。它现在有效,但有人可以解释为什么吗?
describe('Sinon examples', () => {
it('logs test on do-it', () => {
class TestElement extends mixin(HTMLElement) {}
customElements.define('test-element', TestElement);
const spy = sinon.spy(TestElement.prototype, 'doIt');
const el = new TestElement();
el.dispatchEvent(new CustomEvent('do-it'));
expect(spy.calledOnce).to.be.true;
});
});