Skip to content

Latest commit

 

History

History
100 lines (71 loc) · 2.15 KB

Readme.md

File metadata and controls

100 lines (71 loc) · 2.15 KB

minispy

Simple test spy utilities, framework agnostic. Inspection API similar to sinon.js.

Installation

Component

$ component install ericgj/minispy

Node.js

$ npm install minispy

Examples

To use a spy itself as a simple callback function

var spy = Spy();
someFunctionWithCallback("parameter", spy.watch);
assert(spy.called());

To wrap a callback function with a spy

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

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

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, 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, allowing for easily defined custom finders and chaining. For example, to select "the return values from all spied calls with at least one argument":

spy.calls().select('arguments.length > 0').map('returnValue');

License

MIT