I'm trying to spy a method call from a class instance, like this:
export class MyClass {
async method(project, service) {
console.log('calling my method');
}
}
and then on my test, I'm doing like this:
const classInstance = new MyClass();
const spy = sandbox.spy(classInstance, 'method');
classInstance.method();
sinon.assert.calledOnce(spy);
The weird thing is, the "console.log" is printed out, but the sinon assertion is saying that the method is not called :\
Am I doing something wrong? Thanks!
It should work. I am using "sinon": "^8.1.1"
. E.g.
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);
});
});
unit test results:
60605728
calling my method
✓ should spy
1 passing (10ms)