Class: MojoSpy

MojoSpy

new MojoSpy(object, methodName)

Parameters:
Name Type Description
object object

Object containing a method methodName to spy on.

methodName string

The name of the method in object to spy on.

Author:
  • Jonathan Eiten
License:
  • MIT Instantiating a `MojoSpy` object lets you spy on a particular method: * Observe how many times the method was called (records the particular parameters for each call) * Call through to the method * Call through to a mock function instead (which can the call through on its own) The above options are switchable. By default for testing purposes, call history is ON but call through is OFF.

Members

(abstract) callHistory :Array.Array

List of call arguments as arrays.

These are the arguments objects from each call, converted to true Array objects.

Type:
  • Array.Array
Default Value:
  • []

(abstract) callThru :boolean|function

Whether to call through

The three options are:

  • true - call the original (spied-upon) method
  • a mock - call a mock instead
  • false - do not call anything

A mock can call the original method by calling:

spy.method.apply(this, Array.prototype.slice.call(arguments));

Note that the context in the above (this and arguments) is the mock function body.

Type:
  • boolean | function
Default Value:
  • false

(abstract) isRecording :boolean

Whether or not to record calls to the original method.

If truthy, calls to the original method are recorded in this.callHistory.

Type:
  • boolean
Default Value:
  • true
See:
  • on shorthand to turn historical recording **ON**.
  • off shorthand to turn historical recording **OFF**.

Methods

clear()

Resets the call history to an empty list.

off()

Turns historical recording OFF.

on()

Turns historical recording ON.

reset(object, methodName)

Resets options.

This method resets the options to their default values:

  • Record calling history
  • Block call-throughs
  • Empty the history list

Note: This method does not disentangle the spy.

Parameters:
Name Type Description
object
methodName

retire()

synonym: close()

The spied-upon method is restored to its original state. This method is provided for tear down. The spy object is now a lame duck and may be disposed of.

wasCalled() → {boolean}

Determines if (or how) spied-upon menthod was called.

Overloaded:

  • When called without any parameters: Determines whether or not spied-upon method was ever called with or without parameters.
  • When called with some parameters: Determines whether or not spied-upon method was called with exactly the given (expected) parameters. (Looks through this.callHistory for a matching set of parameters.) Hint: If you have the expected parameters in an array, call may with: yourSpy.wasCalled.apply(yourSpy, yourArray).

Note that there is presently no way to determine if the spied-upon method was called without any parameters.

Parameters:
Name Type Attributes Description
arg... * <optional>

"Exact" (i.e., ===) arguments required to have been supplied to the spied-upon method

Returns:

true if spied-upon method called (with exact args).

Type
boolean