Mock.js revision 5aab19b8b78189ce79cf89283f2d505566d4e6a8
/**
* Creates a new mock object.
* @namespace Test
* @class Mock
* @constructor
* @param {Object} template (Optional) An object whose methods
* should be stubbed out on the mock object.
*/
//use blank object is nothing is passed in
var mock,
name;
//try to create mock that keeps prototype chain intact
//fails in the case of ActiveX objects
try {
function f(){}
mock = new f();
} catch (ex) {
mock = {};
}
//create stubs for all methods
return function(){
};
}(name);
}
}
}
//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){
i;
//save expectations
//process arguments
args[i] = Test.Mock.Value(Test.Assert.areSame, [args[i]], "Argument " + i + " of " + name + "() is incorrect.");
}
}
//if the method is expected to be called
if (callCount > 0){
try {
Test.Assert.areEqual(args.length, arguments.length, "Method " + name + "() passed incorrect number of arguments.");
}
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) {
Test.Assert.areEqual(expectation.callCount, expectation.actualCallCount, "Method " + expectation.method + "() wasn't called the expected number of times.");
} else if (expectation.property){
Test.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
}
};
/**
* Creates a new value matcher.
* @param {Function} method The function to call on the value.
* @param {Array} originalArgs (Optional) Array of arguments to pass to the method.
* @param {String} message (Optional) Message to display in case of failure.
* @namespace Test.Mock
* @class Value
* @constructor
*/
};
} else {
}
};
/**
* Predefined matcher to match any value.
* @property Any
* @static
* @type Function
*/
/**
* Predefined matcher to match boolean values.
* @property Boolean
* @static
* @type Function
*/
/**
* Predefined matcher to match number values.
* @property Number
* @static
* @type Function
*/
/**
* Predefined matcher to match string values.
* @property String
* @static
* @type Function
*/
/**
* Predefined matcher to match object values.
* @property Object
* @static
* @type Function
*/
/**
* Predefined matcher to match function values.
* @property Function
* @static
* @type Function
*/