Sinon version: 2.4.1 and 3.0.0 Environment: Node 8 (also happens in earlier versions of Node) What did you expect to happen?
The nice thing about adding the property to the sandbox is that sinon then helps me keep my global test environment clean between each test via sandbox.restore().It is an extremely useful feature, especially when dealing with 3rd party libraries like Google Maps where I don't control the API. Meanwhile, testdouble.js functions are intended to play either role. stub () // verifying an invocation honk = sinon. But when I use sandbox.stub() it does not work. Some experiments with sinon spy and stub.restore() and how you might implement it for global solution. Standalone test spies, stubs and mocks for JavaScript. sinon.spy becomes this.spy; sinon.stub becomes this.stub; sinon.mock becomes this.mock; Async Tests with sinon.test. Sinon sandbox makes stubbing much easier. We'll use Sinon.js to stub a response from a JSON API that retrieves a list of photos in an album. var sandbox = sinon.createSandbox(config); The sinon.createSandbox(config) method is often an integration feature, and can be used for scenarios including a global object to coordinate all fakes through. Some experiments with sinon spy and stub.restore() and how you might implement it for global solution. sinon restore all stubs and spys: ... context. Methods and properties are restored after test(s) are run. sandbox. Here we see an immediate limitation of Sinon’s coupling of the stub/spy/mock roles with their means of creation, as it only provides this convenience for stubs. sandbox = sinon. Calling behavior defining methods like returns or throws multiple times overrides the behavior of the stub. If the property doesn't exist you don't need to add it to the sandbox. - stub-properties-and-methods-sinon.js Let's say we have this function that updates the database: In order to restore your sinon stubs after every test, call restore in an afterEach hook. Sinon-sandbox is a testing utility to make it easy to restore stubs after every test, agnostic of your team's testing framework of choice. Works with any unit testing framework. spy () // pre-asserting an invocation honk = sinon. You may need to disable fake timers for async tests when using sinon.test. Sinon.JS used to stub properties and methods in a sandbox. If you would like to learn more about testing with Mocha and Chai before continuing, you can follow our guide.
Wrapping a test with sinon.test () allows us to use Sinon’s sandboxing feature, allowing us …