Mock.js revision 8e2f5a54575e4da16c524b6c45cba19a4ad00070
/**
* Creates a new mock object.
* @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 = 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
};
}
} 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
*/
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.");
}
});
};
};
} else {
}
};