minispy

Simple test spy, framework-agnostic

minispy

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

$ component install ericgj/minispy
$ npm install minispy
var spy = Spy();
someFunctionWithCallback("parameter", spy.watch);
assert(spy.called());
var spy = Spy(callback);
someFunctionWithCallback("parameter", spy.watch);
assert.equal(spy.lastCall().returnValue, callback());
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.

var spy = Spy.wrap(obj, "method", function(){
  obj.method();
});
assert.equal(spy.lastCall().returnValue, obj.method());

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');

MIT