1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
6 * Create a mock function that records function calls and validates against
10 function MockMethod() {
12 var args
= Array
.prototype.slice
.call(arguments
);
14 return this.returnValue
;
18 * List of signatures for fucntion calls.
19 * @type {!Array.<!Array>}
25 * List of expected call signatures.
26 * @type {!Array.<!Array>}
29 fn
.expectations_
= [];
32 * Value returned from call to function.
35 fn
.returnValue
= undefined;
37 fn
.__proto__
= MockMethod
.prototype;
41 MockMethod
.prototype = {
43 * Adds an expected call signature.
44 * @param {...} var_args Expected arguments for the function call.
46 addExpectation: function() {
47 var args
= Array
.prototype.slice
.call(arguments
);
48 this.expectations_
.push(args
);
52 * Adds a call signature.
53 * @param {!Array} args.
55 recordCall: function(args
) {
56 this.calls_
.push(args
);
60 * Verifies that the function is called the expected number of times and with
61 * the correct signature for each call.
63 verifyMock: function() {
64 var errorMessage
= 'Number of method calls did not match expectation.';
65 if (this.functionName
)
66 errorMessage
= 'Error in ' + this.functionName
+ ':\n' + errorMessage
;
67 assertEquals(this.expectations_
.length
,
70 for (var i
= 0; i
< this.expectations_
.length
; i
++) {
71 this.validateCall(i
, this.expectations_
[i
], this.calls_
[i
]);
76 * Verifies that the observed function arguments match expectations.
77 * Override if strict equality is not required.
78 * @param {number} index Canonical index of the function call. Unused in the
79 * base implementation, but provides context that may be useful for
81 * @param {!Array} expected The expected arguments.
82 * @parma {!Array} observed The observed arguments.
84 validateCall: function(index
, expected
, observed
) {
85 assertDeepEquals(expected
, observed
);
90 * Controller for mocking methods. Tracks calls to mocked methods and verifies
91 * that call signatures match expectations.
94 function MockController() {
96 * Original functions implementations, which are restored when |reset| is
98 * @type {!Array.<!Object>}
101 this.overrides_
= [];
104 * List of registered mocks.
105 * @type {!Array.<!MockMethod>}
111 MockController
.prototype = {
113 * Creates a mock function.
114 * @param {Object=} opt_parent Optional parent object for the function.
115 * @param {string=} opt_functionName Optional name of the function being
116 * mocked. If the parent and function name are both provided, the
117 * mock is automatically substituted for the original and replaced on
120 createFunctionMock: function(opt_parent
, opt_functionName
) {
121 var fn
= new MockMethod();
124 if (opt_parent
&& opt_functionName
) {
125 this.overrides_
.push({
127 functionName
: opt_functionName
,
128 originalFunction
: opt_parent
[opt_functionName
]
130 opt_parent
[opt_functionName
] = fn
;
131 fn
.functionName
= opt_functionName
;
133 this.mocks_
.push(fn
);
139 * Validates all mocked methods. An exception is thrown if the
140 * expected and actual calls to a mocked function to not align.
142 verifyMocks: function() {
143 for (var i
= 0; i
< this.mocks_
.length
; i
++) {
144 this.mocks_
[i
].verifyMock();
149 * Discard mocks reestoring default behavior.
152 for (var i
= 0; i
< this.overrides_
.length
; i
++) {
153 var override
= this.overrides_
[i
];
154 override
.parent
[override
.functionName
] = override
.originalFunction
;