mi6
A library for making spies.
Example
var Mi6 = ;var spy = ; spy// orspy;// orvar spy = ;spy; ; to;to;to; ;to; ;to;tobe;to;
Building Spies
Mi6 exposes helpers to overwrite methods on objects, and later restore them.
var Obj = { };var Mi6 = ; var spy = ; spy;Obj; // returns 5; Objfn // Restores fn to original fn
If a spy was created this way, you may later restore the original function on the object by calling spy.restore()
.
Working with Spies
Configuring Behavior
#calls(fn)
Makes a spy call fn
.
var Spy = Spy;var spy = ;var called = false;spy;; // returns 2, called == true;
#callsThrough()
Makes a spy call the function it was initiantiated with.
var { return 5;};var spy = original;spy; == 5; // true
#returns(val)
Has a spy return a value rather than call a function.
var spy = ;spy; == 5; // true
Testing Methods
#called()
Returns true
if spy has been called, otherwise returns false
.
var spy = ;;to;
#callCount()
Returns num
where num is the number of times spy has been called.
var spy = ;;;;to;
#calledWith(...args)
If ...args
is not specified, returns an array of what the spy was called with.
If ...args
is specified, returns true
if the spy was called with those arguments, otherwise it returns false
.
var spy = ;;to;;to;
Manipulation methods
#reset()
Resets the states of the spy for things like called
, callCount
and calledWith
.
var spy = ;;spy;to;to;