SuperSpyBuilder

SuperSpyBuilder

Builder class that can be used to inject an intermediate spy between a parent class and child class, making it possible to spy on super() calls made by the child. The builder also supports injection of mocks on the parent class' prototype, enabling spying on calls made by the child class on methods inherited from the parent class. The class exposes methods that will allow the mock/spy references to be torn down after testing. Calls to super() will automatically be spied upon, and the parent class' super() method will be called after the spy has been called.

Constructor

new SuperSpyBuilder(ParentClass, ChildClass)

Source:
Parameters:
Name Type Description
ParentClass Class The parent class
ChildClass Class The child class

Members

mocks :Object

Source:
An object containing references to stub instances for each of the methods on the parent that has been mocked. The stubs returned are references to sinon stubs.
Type:
  • Object

Methods

addMock(method, fakeopt, skipSuperMethodopt) → {Object}

Source:
Creates a mock on the parent class. The mock method intercepts any calls to the parent method, and logs the invocation against the corresponding mock spy reference.

The mocks are not applied to the parent class' prototype until the SuperSpyBuilder.inject method is called.

Parameters:
Name Type Attributes Default Description
method String The name of the method on the parent on which to apply the mock. If a value of "super()" is passed, an error will be thrown. This method must be defined by the parent class.
fake function <optional>
A fake function that will be invoked every time the parent method is called. If possible, the function will be invoked in the context of the instance with a valid this parameter. This will not work if the fake is an arrow function
skipSuperMethod Boolean <optional>
false A boolean that indicates if the actual method on the parent should be invoked after the fake is invoked. The parent method will be invoked by default.
Returns:
A reference to this object (can be used to chain method calls)
Type
Object

inject() → {SuperSpy}

Source:
Generates and injects a spy class between a parent and child class, changing the inheritance hierarchy from:

ParentClass --> Child hierarchy

to:

ParentClass --> SuperSpy --> Child

Additionally, this method modifies the parent class prototype, creating mocks as defined by calls to addMock().

Returns:
A reference to the spy class, that is injected between the parent and the child.
Type
SuperSpy

restore()

Source:
Restores the relationship between parent and child, by removing the spy class from the hierarchy, changing the inheritance hierarchy from:

ParentClass --> SuperSpy --> Child

to:

ParentClass --> Child

Any mocks injected into the parent class' prototype are also removed, restoring the Parent prototype to its original form.