# Class goog.testing.Mock

code »

All implemented interfaces:
  `goog.testing.MockInterface`

The base class for a mock object.

## Constructor

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

Parameters

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.

Show:Public Protected Private

## Instance Methods

code »$anyTimes ( ) ⇒ `!goog.testing.Mock`

Allows the expectation to be called any number of times.

Returns

This mock object.

code »$argumentsAsString ( args ) ⇒ `string`

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

Parameters

args: `?Array`
  The arguments passed to the mock.

Returns

Human-readable string.

code »$atLeastOnce ( ) ⇒ `!goog.testing.Mock`

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

Returns

This mock object.

code »$atMostOnce ( ) ⇒ `!goog.testing.Mock`

Allows the expectation to be called 0 or 1 times.

Returns

This mock object.

code »$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.

Parameters

expectation: `goog.testing.MockExpectation`
  The expectation.
args: `Array`
  The arguments to the method.

Returns

The return value expected by the mock.

code »$does ( func ) ⇒ `!goog.testing.Mock`

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

Parameters

func: `Function`
  The function to call.

Returns

This mock object.

code »$initializeFunctions_ ( objectToMock )

Initializes the functions on the mock object.

Parameters

objectToMock: `Object`
  The object being mocked.

code »$maybeThrow ( expectation )

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

Parameters

expectation: `goog.testing.MockExpectation`
  The expectation.

code »$mockMethod ( name ) ⇒ `*`

The function that replaces all methods on the mock object.

Parameters

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.

code »$never ( ) ⇒ `!goog.testing.Mock`

Disallows the expectation from being called.

Returns

This mock object.

code »$once ( ) ⇒ `!goog.testing.Mock`

Allows the expectation to be called exactly once.

Returns

This mock object.

code »$recordAndThrow ( ex )

Throws an exception and records that an exception was thrown.

Parameters

ex: `Object`
  Exception.

Throws

`Object`
  #ex.

code »$recordCall ( name, args ) ⇒ `*`

Records an actual method call, intended to be overridden by a subclass. The subclass must find the pending expectation and return the correct value.

Parameters

name: `string`
  The name of the method being called.
args: `Array`
  The arguments to the method.

Returns

The return expected by the mock.

code »$recordExpectation ( )

Records the currently pending expectation, intended to be overridden by a subclass.

code »$registerArgumentListVerifier ( methodName, fn ) ⇒ `!goog.testing.Mock`

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

Parameters

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

This mock object.

code »$replay ( )

Switches from recording to replay mode.

code »$reset ( )

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

code »$returns ( val ) ⇒ `!goog.testing.Mock`

Specifies a return value for the currently pending expectation.

Parameters

val: `*`
  The return value.

Returns

This mock object.

code »$throwCallException ( name, args, opt_expectation )

Throw an exception based on an incorrect method call.

Parameters

name: `string`
  Name of method called.
args: `?Array`
  Arguments passed to the mock.
opt_expectation: `goog.testing.MockExpectation=`
  Expected next call, if any.

code »$throwException ( comment, opt_message )

Throws an exception and records that an exception was thrown.

Parameters

comment: `string`
  A short comment about the exception.
opt_message: `?string=`
  A longer message about the exception.

Throws

`Object`
  JsUnitException object.

code »$throws ( val ) ⇒ `!goog.testing.Mock`

Specifies a value for the currently pending expectation to throw.

Parameters

val: `*`
  The value to throw.

Returns

This mock object.

code »$times ( times ) ⇒ `!goog.testing.Mock`

Specifies the number of times the expectation should be called.

Parameters

times: `number`
  The number of times this method will be called.

Returns

This mock object.

code »$verify ( )

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

code »$verifyCall ( expectation, name, args ) ⇒ `boolean`

Verifies that a method call matches an expectation.

Parameters

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

Whether the call matches the expectation.

## Instance Properties

code »$argumentListVerifiers_ : `Object`

Map of argument name to optional argument list verifier function.

code »$pendingExpectation : `goog.testing.MockExpectation`

The expectation currently being created. All methods that modify the current expectation return the Mock object for easy chaining, so this is where we keep track of the expectation that's currently being modified.

code »$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.

code »$recording_ : `boolean`

Whether or not we are in recording mode.

code »$threwException_ : `Object`

First exception thrown by this mock; used in $verify.

## Static Properties

code »goog.testing.Mock.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.

code »goog.testing.Mock.PROTOTYPE_FIELDS_ : `!Array.<string>`

This array contains the name of the functions that are part of the base Object prototype. Basically a copy of goog.object.PROTOTYPE_FIELDS_.

code »goog.testing.Mock.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.