SinonJS 间谍调用似乎无法正常工作
SinonJS spy call seems not working correctly
我正在尝试侦测来自 class 实例的方法调用,如下所示:
export class MyClass {
async method(project, service) {
console.log('calling my method');
}
}
然后在我的测试中,我是这样做的:
const classInstance = new MyClass();
const spy = sandbox.spy(classInstance, 'method');
classInstance.method();
sinon.assert.calledOnce(spy);
奇怪的是,"console.log" 被打印出来了,但是 sinon 断言说这个方法没有被调用:\
我是不是做错了什么?谢谢!
应该可以。我正在使用 "sinon": "^8.1.1"
。例如
index.ts
:
export class MyClass {
async method(project, service) {
console.log('calling my method');
}
}
index.test.ts
:
import { MyClass } from './';
import sinon from 'sinon';
describe('60605728', () => {
it('should spy', () => {
const sandbox = sinon.createSandbox();
const classInstance = new MyClass();
const spy = sandbox.spy(classInstance, 'method');
classInstance.method('project', 'service');
sinon.assert.calledOnce(spy);
});
});
单元测试结果:
60605728
calling my method
✓ should spy
1 passing (10ms)
我正在尝试侦测来自 class 实例的方法调用,如下所示:
export class MyClass {
async method(project, service) {
console.log('calling my method');
}
}
然后在我的测试中,我是这样做的:
const classInstance = new MyClass();
const spy = sandbox.spy(classInstance, 'method');
classInstance.method();
sinon.assert.calledOnce(spy);
奇怪的是,"console.log" 被打印出来了,但是 sinon 断言说这个方法没有被调用:\
我是不是做错了什么?谢谢!
应该可以。我正在使用 "sinon": "^8.1.1"
。例如
index.ts
:
export class MyClass {
async method(project, service) {
console.log('calling my method');
}
}
index.test.ts
:
import { MyClass } from './';
import sinon from 'sinon';
describe('60605728', () => {
it('should spy', () => {
const sandbox = sinon.createSandbox();
const classInstance = new MyClass();
const spy = sandbox.spy(classInstance, 'method');
classInstance.method('project', 'service');
sinon.assert.calledOnce(spy);
});
});
单元测试结果:
60605728
calling my method
✓ should spy
1 passing (10ms)