Mock.js revision 0c9749c3ca70539e41104900fede9cbfa4328678
/**
* Creates a new mock object.
* @class Mock
* @constructor
* @param {Object} template (Optional) An object whose methods
* should be stubbed out on the mock object. This object
* is used as the prototype of the mock object so instanceof
* works correctly.
*/
//use blank object is nothing is passed in
var mock = null;
//try to create mock that keeps prototype chain intact
try {
} catch (ex) {
mock = {};
}
//create new versions of the methods so that they don't actually do anything
};
}
});
//return it
return mock;
};
/**
* Assigns an expectation to a mock object. This is used to create
* methods and properties on the mock object that are monitored for
* calls and changes, respectively.
* @param {Object} mock The object to add the expectation to.
* @param {Object} expectation An object defining the expectation. For
* a method, the keys "method" and "args" are required with
* an optional "returns" key available. For properties, the keys
* "property" and "value" are required.
* @return {void}
* @method expect
* @static
*/
//make sure there's a place to store the expectations
if (!mock.__expectations) {
mock.__expectations = {};
}
//method expectation
if (expectation.method){
//save expectations
//process arguments
array[i] = Y.Mock.Value(Y.Assert.areSame, [arg], "Argument " + i + " of " + name + "() is incorrect.");
}
});
//if the method is expected to be called
if (callCount > 0){
try {
Y.Assert.areEqual(args.length, arguments.length, "Method " + name + "() passed incorrect number of arguments.");
//if (args[i]){
//} else {
// Y.Assert.fail("Argument " + i + " (" + arguments[i] + ") was not expected to be used.");
//}
}
if (error){
throw error;
}
} catch (ex){
//route through TestRunner for proper handling
}
return result;
};
} else {
//method should fail if called when not expected
try {
} catch (ex){
//route through TestRunner for proper handling
}
};
}
} else if (expectation.property){
//save expectations
}
};
/**
* Verifies that all expectations of a mock object have been met and
* throws an assertion error if not.
* @param {Object} mock The object to verify..
* @return {void}
* @method verify
* @static
*/
try {
if (expectation.method) {
Y.Assert.areEqual(expectation.callCount, expectation.actualCallCount, "Method " + expectation.method + "() wasn't called the expected number of times.");
} else if (expectation.property){
Y.Assert.areEqual(expectation.value, mock[expectation.property], "Property " + expectation.property + " wasn't set to the correct value.");
}
});
} catch (ex){
//route through TestRunner for proper handling
}
};
/**
* Defines a custom mock validator for a particular argument.
* @param {Function} method The method to run on the argument. This should
* throw an assertion error if the value is invalid.
* @param {Array} originalArgs The first few arguments to pass in
* to the method. The value to test and failure message are
* always the last two arguments passed into method.
* @param {String} message The message to display if validation fails. If
* not specified, the default assertion error message is displayed.
* @return {void}
* @namespace Mock
* @constructor Value
* @static
*/
};
} else {
}
};
/**
* Mock argument validator that accepts any value as valid.
* @namespace Mock.Value
* @property Any
* @type Function
* @static
*/
/**
* Mock argument validator that accepts only Boolean values as valid.
* @namespace Mock.Value
* @property Boolean
* @type Function
* @static
*/
/**
* Mock argument validator that accepts only numeric values as valid.
* @namespace Mock.Value
* @property Number
* @type Function
* @static
*/
/**
* Mock argument validator that accepts only String values as valid.
* @namespace Mock.Value
* @property String
* @type Function
* @static
*/
/**
* Mock argument validator that accepts only non-null objects values as valid.
* @namespace Mock.Value
* @property Object
* @type Function
* @static
*/
/**
* Mock argument validator that accepts onlyfunctions as valid.
* @namespace Mock.Value
* @property Function
* @type Function
* @static
*/