# Class goog.testing.LooseMock

code »

    goog.testing.Mock
      └ goog.testing.LooseMock

All implemented interfaces:
  `goog.testing.MockInterface`

This is a mock that does not care about the order of method calls. As a result, it won't throw exceptions until verify() is called. The only exception is that if a method is called that has no expectations, then an exception will be thrown.

## Constructor

goog.testing.LooseMock ( objectToMock, opt_ignoreUnexpectedCalls, opt_mockStaticMethods, opt_createProxy )

Parameters

objectToMock: `(Object|Function)`
  The object that should be mocked, or the constructor of an object to mock.
opt_ignoreUnexpectedCalls: `boolean=`
  Whether to ignore unexpected calls.
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

### Defined in `goog.testing.LooseMock`

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

Parameters

name
args

code »$recordExpectation ( )
code »$replay ( )
code »$reset ( )
code »$setIgnoreUnexpectedCalls ( ignoreUnexpectedCalls ) ⇒ `!goog.testing.LooseMock`

A setter for the ignoreUnexpectedCalls field.

Parameters

ignoreUnexpectedCalls: `boolean`
  Whether to ignore unexpected calls.

Returns

This mock object.

code »$verify ( )

### Defined in `goog.testing.Mock`

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

### Defined in `goog.testing.LooseMock`

code »$calls_ : `Array.<Array>`

The calls that have been made; we cache them to verify at the end. Each element is an array where the first element is the name, and the second element is the arguments.

code »$expectations_ : `goog.structs.Map`

A map of method names to a LooseExpectationCollection for that method.

code »$ignoreUnexpectedCalls_ : `boolean`

Whether to ignore unexpected calls.

### Defined in `goog.testing.Mock`

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.LooseMock.superClass_ : `goog.testing.Mock.prototype`