The goog.testing.Mock Class

goog.testing.Mock(objectToMock, opt_mockStaticMethods, opt_createProxy)

The base class for a mock object.

objectToMock {Object|Function}
The object that should be mocked, or the constructor of an object to mock.
opt_mockStaticMethods {boolean=}
An optional argument denoting that a mock should be constructed from the static functions of a class.
opt_createProxy {boolean=}
An optional argument denoting that a proxy for the target mock should be created.

.$anyTimes()

Allows the expectation to be called any number of times.

returns {goog.testing.Mock}
This mock object.

.$argumentListVerifiers_ {Object}

Map of argument name to optional argument list verifier function.

.$argumentsAsString(args)

Render the provided argument array to a string to help clients with debugging tests.

args {Array.<*>?}
The arguments passed to the mock.
returns {string}
Human-readable string.

.$atLeastOnce()

Allows the expectation to be called any number of times, as long as it's called once.

returns {goog.testing.Mock}
This mock object.

.$atMostOnce()

Allows the expectation to be called 0 or 1 times.

returns {goog.testing.Mock}
This mock object.

.$do(expectation, args)

If this expectation defines a function to be called, it will be called and its result will be returned. Otherwise, if the expectation expects to throw, it will throw. Otherwise, this method will return defined value.

expectation {goog.testing.MockExpectation}
The expectation.
args {Array}
The arguments to the method.
returns {*}
The return value expected by the mock.

.$does(func)

Specifies a function to call for currently pending expectation. Note, that using this method overrides declarations made using $returns() and $throws() methods.

func {Function}
The function to call.
returns {goog.testing.Mock}
This mock object.

.$maybeThrow(expectation)

If the expectation expects to throw, this method will throw.

expectation {goog.testing.MockExpectation}
The expectation.

.$mockMethod(name)

The function that replaces all methods on the mock object.

name {string}
The name of the method being mocked.
returns {*}
In record mode, returns the mock object. In replay mode, returns whatever the creator of the mock set as the return value.

.$proxy {Object}

A proxy for the mock. This can be used for dependency injection in lieu of the mock if the test requires a strict instanceof check.

.$registerArgumentListVerifier(methodName, fn)

Registers a verfifier function to use when verifying method argument lists.

methodName {string}
The name of the method for which the verifierFn should be used.
fn {Function}
Argument list verifier function. Should take 2 argument arrays as arguments, and return true if they are considered equivalent.
returns {goog.testing.Mock}
This mock object.

.$replay()

Switches from recording to replay mode.

.$reset()

Resets the state of this mock object. This clears all pending expectations without verifying, and puts the mock in recording mode.

.$returns(val)

Specifies a return value for the currently pending expectation.

val {*}
The return value.
returns {goog.testing.Mock}
This mock object.

.$throwCallException(name, args, opt_expectation)

Throw an exception based on an incorrect method call.

name {string}
Name of method called.
args {Array.<*>?}
Arguments passed to the mock.
opt_expectation {goog.testing.MockExpectation=}
Expected next call, if any.

.$throws(val)

Specifies a value for the currently pending expectation to throw.

val {*}
The value to throw.
returns {goog.testing.Mock}
This mock object.

.$times(times)

Specifies the number of times the expectation should be called.

times {number}
The number of times this method will be called.
returns {goog.testing.Mock}
This mock object.

.$verify()

Verify that all of the expectations were met. Should be overridden by subclasses.

.$verifyCall(expectation, name, args)

Verifies that a method call matches an expectation.

expectation {goog.testing.MockExpectation}
The expectation to check.
name {string}
The name of the called method.
args {Array.<*>?}
The arguments passed to the mock.
returns {boolean}
Whether the call matches the expectation.

.LOOSE {number}

Option that may be passed when constructing function, method, and constructor mocks. Indicates that the expected calls should be accepted in any order.

@const

.STRICT {number}

Option that may be passed when constructing function, method, and constructor mocks. Indicates that the expected calls should be accepted in the recorded order only.

@const