Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ericgj/minispy
Simple test spy, framework-agnostic
https://github.com/ericgj/minispy
Last synced: 9 days ago
JSON representation
Simple test spy, framework-agnostic
- Host: GitHub
- URL: https://github.com/ericgj/minispy
- Owner: ericgj
- Created: 2013-05-11T19:35:36.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2013-05-12T03:52:13.000Z (over 11 years ago)
- Last Synced: 2024-11-15T12:27:19.951Z (2 months ago)
- Language: JavaScript
- Size: 156 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: Readme.md
- Changelog: History.md
Awesome Lists containing this project
README
# minispy
Simple test spy utilities, framework agnostic.
Inspection API similar to [sinon.js][a].## Installation
### Component
$ component install ericgj/minispy
### Node.js
$ npm install minispy
## Examples
### To use a spy itself as a simple callback function
```javascript
var spy = Spy();
someFunctionWithCallback("parameter", spy.watch);
assert(spy.called());
```### To wrap a callback function with a spy
```javascript
var spy = Spy(callback);
someFunctionWithCallback("parameter", spy.watch);
assert.equal(spy.lastCall().returnValue, callback());
```### To stub a method call on an existing object with a spy
```javascript
var spy = Spy.stub(obj, "method", function(){
obj.method();
});
assert(spy.called());
```Note that `stub` does not _pass through_ the method call but _intercepts_ it.
The injected spy thus acts more like a classical mock.### To wrap a method call on an existing object with a spy
```javascript
var spy = Spy.wrap(obj, "method", function(){
obj.method();
});
assert.equal(spy.lastCall().returnValue, obj.method());
```## API
The API for inspecting spy results is similar to [sinon.js][a],
except using methods instead of properties (e.g. `spy.called()` vs
`spy.called`.The following inspection methods from sinon.js are built-in:
- callCount
- called
- notCalled
- calledOnce
- calledExactly({Integer})
- firstCall
- lastCall
- getCall({Integer})
- calledWith(args...)
- alwaysCalledWith(args...)
- calledWithExactly(args...)
- alwaysCalledWithExactly(args...)
- neverCalledWith(args...)
- threw({null|String|Object})
- alwaysThrew({null|String|Object})
- returned({Object})
- alwaysReturned({Object})
- calledBefore({Spy})
- calledAfter({Spy})Note that `spy.calls()` returns an [enumerable][b], allowing for
easily defined custom finders and chaining. For example, to select
_"the return values from all spied calls with at least one argument"_:```javascript
spy.calls().select('arguments.length > 0').map('returnValue');
```## License
MIT
[a]: http://sinonjs.org/docs/#spies
[b]: https://github.com/component/enumerable