function MockMethod() {
var fn = function() {
var args = Array.prototype.slice.call(arguments);
+ var callbacks =
+ args.filter(function(arg) { return (typeof arg == 'function'); });
+
+ if (callbacks.length > 1) {
+ console.error('Only support mocking function with at most one callback.');
+ return;
+ }
+
fn.recordCall(args);
- return this.returnValue;
+ if (callbacks.length == 1) {
+ callbacks[0].apply(undefined, fn.callbackData);
+ return;
+ }
+ return fn.returnValue;
};
/**
*/
fn.returnValue = undefined;
+ /**
+ * List of arguments for callback function.
+ * @type {!Array.<!Array>}
+ */
+ fn.callbackData = [];
+
fn.__proto__ = MockMethod.prototype;
return fn;
}
*/
addExpectation: function() {
var args = Array.prototype.slice.call(arguments);
- this.expectations_.push(args);
+ this.expectations_.push(args.filter(this.notFunction_));
},
/**
* @param {!Array} args.
*/
recordCall: function(args) {
- this.calls_.push(args);
+ this.calls_.push(args.filter(this.notFunction_));
},
/**
validateCall: function(index, expected, observed) {
assertDeepEquals(expected, observed);
},
+
+ /**
+ * Test if arg is a function.
+ * @param {*} arg The argument to test.
+ * @return True if arg is not function type.
+ */
+ notFunction_: function(arg) {
+ return typeof arg != 'function';
+ }
};
/**