https://github.com/nemtsov/sinon-lazy-stub
Sinon.JS lazy (and efficient) stub creator
https://github.com/nemtsov/sinon-lazy-stub
Last synced: 2 months ago
JSON representation
Sinon.JS lazy (and efficient) stub creator
- Host: GitHub
- URL: https://github.com/nemtsov/sinon-lazy-stub
- Owner: nemtsov
- License: mit
- Created: 2014-06-01T07:48:29.000Z (about 12 years ago)
- Default Branch: master
- Last Pushed: 2014-07-11T03:01:25.000Z (about 12 years ago)
- Last Synced: 2025-07-03T23:27:16.669Z (about 1 year ago)
- Language: JavaScript
- Size: 133 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Sinon.js lazy stub
When using `sinon` you sometimes need to stub an object that has many
methods. This can be taxing on performance, especially if you
re-create the stub before every test (for a clean test).
This module solves this problem by lazily creating the stubs of the
methods of an object, only when they're used.
## Usage
**Simple:**
```javascript
var lazyStub = require('sinon-lazy-stub');
api = {get: function () {}},
createStub = lazyStub(api),
stubA, stubB;
stubA = createStub();
stubB = createStub();
stubA.get.returns(7);
assert.equal(stub.get())
```
**Recommended:**
```javascript
// api-test.js
var stubs = require('./my_stubs');
describe('API', function () {
var api;
beforeEach(function () {
api = stubs.api();
});
it('should be great', function () {
//...
});
});
// my_stubs.js
var lazyStub = require('sinon-lazy-stub'),
api = require('...');
module.exports = {
api: lazyStub(api)
};
```
As you see in the case above, I recommend creating a separate
file in your project with the stubs. That way they're all in
a single known place.
## License
MIT. See LICENSE