goog.testing.Mock
Provided By | |
---|---|
All Implemented Interfaces | |
All Known Direct Subclasses |
The base class for a mock object.
new Mock( objectToMock, opt_mockStaticMethods, opt_createProxy )
Parameters |
|
---|
Instance Methods
this.$anyTimes() → goog.testing.Mock
goog.testing.Mock
Allows the expectation to be called any number of times.
Parameters | None. | ||
---|---|---|---|
Returns |
|
this.$argumentsAsString( args ) → string
string
this.$atLeastOnce() → goog.testing.Mock
goog.testing.Mock
Allows the expectation to be called any number of times, as long as it's called once.
Parameters | None. | ||
---|---|---|---|
Returns |
|
this.$atMostOnce() → goog.testing.Mock
goog.testing.Mock
Allows the expectation to be called 0 or 1 times.
Parameters | None. | ||
---|---|---|---|
Returns |
|
this.$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 |
| ||||||||
---|---|---|---|---|---|---|---|---|---|
Returns |
|
this.$does( func ) → goog.testing.Mock
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 |
| ||||
---|---|---|---|---|---|
Returns |
|
this.$maybeThrow( expectation ) → void
void
If the expectation expects to throw, this method will throw.
Parameters |
|
---|
this.$mockMethod( name ) → *
*
The function that replaces all methods on the mock object.
Parameters |
| ||||
---|---|---|---|---|---|
Returns |
|
this.$never() → goog.testing.Mock
goog.testing.Mock
Disallows the expectation from being called.
Parameters | None. | ||
---|---|---|---|
Returns |
|
this.$once() → goog.testing.Mock
goog.testing.Mock
Allows the expectation to be called exactly once.
Parameters | None. | ||
---|---|---|---|
Returns |
|
this.$recordAndThrow( ex, rethrow ) → void
void
Throws an exception and records that an exception was thrown.
Parameters |
| ||||||||
---|---|---|---|---|---|---|---|---|---|
Throws |
|
this.$recordCall( name, args ) → *
*
this.$recordExpectation() → void
void
Records the currently pending expectation, intended to be overridden by a subclass.
this.$registerArgumentListVerifier( methodName, fn ) → goog.testing.Mock
goog.testing.Mock
Registers a verifier function to use when verifying method argument lists.
Parameters |
| ||||||||
---|---|---|---|---|---|---|---|---|---|
Returns |
|
this.$replay() → void
void
Switches from recording to replay mode.
Specified by | |
---|---|
Parameters | None. |
this.$reset() → void
void
Resets the state of this mock object. This clears all pending expectations without verifying, and puts the mock in recording mode.
Specified by | |
---|---|
Parameters | None. |
this.$returns( val ) → goog.testing.Mock
goog.testing.Mock
Specifies a return value for the currently pending expectation.
Parameters |
| ||||
---|---|---|---|---|---|
Returns |
|
this.$throwCallException( name, args, opt_expectation ) → void
void
Throw an exception based on an incorrect method call.
Parameters |
|
---|
this.$throwException( comment, opt_message ) → void
void
this.$throws( val ) → goog.testing.Mock
goog.testing.Mock
Specifies a value for the currently pending expectation to throw.
Parameters |
| ||||
---|---|---|---|---|---|
Returns |
|
this.$times( times ) → goog.testing.Mock
goog.testing.Mock
Specifies the number of times the expectation should be called.
Parameters |
| ||||
---|---|---|---|---|---|
Returns |
|
this.$verify() → void
void
Verify that all of the expectations were met. Should be overridden by subclasses.
Specified by | |
---|---|
Parameters | None. |
this.$verifyCall( expectation, name, args ) → boolean
boolean
Verifies that a method call matches an expectation.
Parameters |
| ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Returns |
|
this.$waitAndVerify() → goog.Promise<undefined, ?>
goog.Promise<undefined, ?>
Waits for the Mock to gather expectations and then performs verify.
Specified by | ||
---|---|---|
Parameters | None. | |
Returns |
|
Instance Properties
this.$argumentListVerifiers_ → (Object|null)
(Object|null)
Map of argument name to optional argument list verifier function.
this.$pendingExpectation → (goog.testing.MockExpectation|null)
(goog.testing.MockExpectation|null)
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.
this.$proxy → (Object|null)
(Object|null)
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.
this.waitingForExpectations → (goog.promise.Resolver<undefined>|null)
(goog.promise.Resolver<undefined>|null)
No information.
Static Functions
Mock.record( obj ) → ?
?
Asserts that a mock object is in record mode. This avoids type system errors from mock expectations.
Usage:
const record = goog.require('goog.testing.Mock.record');
record(mockObject).someMethod(ignoreArgument).$returns(42);
record(mockFunction)(ignoreArgument).$returns(42);
Parameters |
| ||||
---|---|---|---|---|---|
Returns |
|
Static Properties
Mock.LOOSE → number
number
Option that may be passed when constructing function, method, and constructor mocks. Indicates that the expected calls should be accepted in any order.
Mock.STRICT → number
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.