minispy

Simple test spy, framework-agnostic

npm install minispy
1 downloads in the last day
1 downloads in the last week
5 downloads in the last month

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

npm loves you