import { SinonSandbox } from 'sinon'; describe('MyClass', function() { let sandbox: SinonSandbox; beforeEach(function() { sandbox = sinon.createSandbox(); }); afterEach(function() { sandbox.restore(); }); it('should do something', function() { const myFake = sinon.fake(); sandbox.replace(MyClass.prototype, 'myMethod', myFake); const myClass = new MyClass(); myClass.doSomething(); expect(myFake).to.have.been.called; }); });
import { SinonSandbox } from 'sinon'; describe('MyClass', function() { let sandbox: SinonSandbox; beforeEach(function() { sandbox = sinon.createSandbox(); }); afterEach(function() { sandbox.restore(); }); it('should call myCallback', function() { const myCallback = sandbox.stub(); const myClass = new MyClass(); myClass.doSomethingAsync(myCallback); sinon.assert.calledOnce(myCallback); }); });In this example, we create a Sinon sandbox before each test case and restore it after each test case. We then use the `sandbox.stub` method to create a stub function for `myCallback`. We then create an instance of `MyClass` and call its `doSomethingAsync` method, passing in `myCallback`. Finally, we use `sinon.assert` to ensure that `myCallback` was called once. Package library: sinon.