PromiseMock

PromiseMock

Class that creates a mock method on an object, but is specifically designed for methods that return a promise. Extends the Mock class and provides additional promise specific functionality.

This class is not meant to be instantiated directly, but is designed for use within the ObjectMock class.

Constructor

new PromiseMock(instance, methodName)

Source:
Parameters:
Name Type Description
instance Object The object instance on which the method will be mocked.
methodName String The name of the method on the object that needs to be mocked. If the specified method does not exist, a placeholder method will be injected into the instance which will then be mocked.

Extends

Members

instance :Object

Source:
Inherited From:
Returns a reference to the instance object that has the mocks applied to it.
Type:
  • Object

methodName :String

Source:
Inherited From:
Returns the name of the mock, which is the name of the method that has been mocked.
Type:
  • String

responses :Array

Source:
Inherited From:
Returns a list of responses returned by the mock up to the current time. Responses may be promises depending on how the mock has been configured.
Type:
  • Array

ret :*|Error

Source:
Inherited From:
Returns the response from the first mock invocation. If the mock has not been invoked yet, an Error object will be returned.
Type:
  • * | Error

stub :Object

Source:
Inherited From:
Returns a reference to the stub method generated by this class. This property can be used to examine the call history and parameters of the mocked method.
Type:
  • Object

Methods

promise(callIndexopt) → {Promise}

Source:
Returns the promise associated with a specific invocation of the mock, identified by the call index. This value could reference a future invocation (meaning that the specific invocation of the mock has not yet occurred). Promises for all completed invocations of the mock can be obtained by inspecting the responses method.

The value of this method is that actions can be be assigned to the then(...) callback of the mock even before the mock has been invoked. The use of the responses array is retroactive in nature, and cannot be accessed until after the method has been invoked, which does not always work in asynchronous scenarios.

Parameters:
Name Type Attributes Default Description
callIndex Number <optional>
0 The index of the invocation, with the first invocation starting at index 0. Defaults to 0.
Returns:
The promise associated with the specified call index.
Type
Promise

reject(erroropt, callIndexopt) → {Promise}

Source:
Rejects the promise associated with a specific invocation of the mock, identified by the call index. This value could reference a future invocation (meaning that the specific invocation of the mock has not yet occurred).

The value of this method is that a rejection can be set on a specific invocation prior to it actually occurring.

Parameters:
Name Type Attributes Default Description
error * <optional>
The rejection response for the promise. This is typically an error, but can be any value.
callIndex Number <optional>
0 The index of the invocation, with the first invocation starting at index 0. Defaults to 0.
Returns:
The promise associated with the specified call index.
Type
Promise

reset()

Source:
Inherited From:
Clears all responses that have been tracked up to this point, and also resets the mock's call history.

resolve(responseopt, callIndexopt) → {Promise}

Source:
Resolves the promise associated with a specific invocation of the mock, identified by the call index. This value could reference a future invocation (meaning that the specific invocation of the mock has not yet occurred).

The value of this method is that a resolution can be set on a specific invocation prior to it actually occurring.

Parameters:
Name Type Attributes Default Description
response * <optional>
The resolution response for the promise. This is typically an error, but can be any value.
callIndex Number <optional>
0 The index of the invocation, with the first invocation starting at index 0. Defaults to 0.
Returns:
The promise associated with the specified call index.
Type
Promise