Permalink
Browse files

Merged in pull request from Mojito

  • Loading branch information...
davglass committed Jul 24, 2012
1 parent de3b488 commit 4670f9912367c5a39e4b11a430e47b8c2c380fd3
Showing with 129 additions and 8 deletions.
  1. +18 −8 src/test/js/Mock.js
  2. +111 −0 src/test/tests/mock-tests.js
View
@@ -48,9 +48,16 @@ YUITest.Mock = function(template){
* 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.
+ * properties, the keys "property" and "value" are required. For a
+ * method the "method" key defines the method's name, the optional "args"
+ * key provides an array of argument types. The "returns" key provides
+ * an optional return value. An optional "run" key provides a function
+ * to be used as the method body. The return value of a mocked method is
+ * determined first by the "returns" key, then the "run" function's return
+ * value. If neither "returns" nor "run" is provided undefined is returned.
+ * An optional 'error' key defines an error type to be thrown in all cases.
+ * The "callCount" key provides an optional number of times the method is
+ * expected to be called (the default is 1).
* @return {void}
* @method expect
* @static
@@ -70,8 +77,9 @@ YUITest.Mock.expect = function(mock /*:Object*/, expectation /*:Object*/){
callCount = (typeof expectation.callCount == "number") ? expectation.callCount : 1,
error = expectation.error,
run = expectation.run || function(){},
+ runResult,
i;
-
+
//save expectations
mock.__expectations[name] = expectation;
expectation.callCount = callCount;
@@ -94,7 +102,7 @@ YUITest.Mock.expect = function(mock /*:Object*/, expectation /*:Object*/){
args[i].verify(arguments[i]);
}
- run.apply(this, arguments);
+ runResult = run.apply(this, arguments);
if (error){
throw error;
@@ -103,8 +111,10 @@ YUITest.Mock.expect = function(mock /*:Object*/, expectation /*:Object*/){
//route through TestRunner for proper handling
YUITest.TestRunner._handleError(ex);
}
-
- return result;
+
+ // Any value provided for 'returns' overrides any value returned
+ // by our 'run' function.
+ return expectation.hasOwnProperty('returns') ? result : runResult;
};
} else {
@@ -221,4 +231,4 @@ YUITest.Mock.Value.Object = YUITest.Mock.Value(YUITest.Assert.isObject);
* @static
* @type Function
*/
-YUITest.Mock.Value.Function = YUITest.Mock.Value(YUITest.Assert.isFunction);
+YUITest.Mock.Value.Function = YUITest.Mock.Value(YUITest.Assert.isFunction);
@@ -874,6 +874,117 @@ YUI.add('mock-tests', function(Y) {
}));
+ //-------------------------------------------------------------------------
+ // Test Case for returns expectations
+ //-------------------------------------------------------------------------
+
+ suite.add(new Y.Test.Case({
+
+ name : "Returns Tests",
+ groups: ["mock", "common"],
+
+ /*
+ * Test that when no 'returns' expectation is given it is undefined.
+ */
+ "Value for 'returns' should default to undefined": function(){
+ var mock = Y.Test.Mock(),
+ result;
+
+ Y.Test.Mock.expect(mock, {
+ method: "method"
+ });
+
+ result = mock.method();
+ Assert.isUndefined(result);
+ },
+
+ /*
+ * Test that when a 'returns' expectation is given it is used.
+ */
+ "Value for 'returns' should be used as return value": function(){
+ var mock = Y.Test.Mock(),
+ result;
+
+ Y.Test.Mock.expect(mock, {
+ method: "method",
+ returns: true
+ });
+
+ result = mock.method();
+ Assert.isTrue(result);
+ },
+
+ /*
+ * Test that when a 'returns' expectation is given it is used regardless
+ * of the return value of any run function provided.
+ */
+ "Value for 'returns' should be used rather than run value": function(){
+ var mock = Y.Test.Mock(),
+ result;
+
+ Y.Test.Mock.expect(mock, {
+ method: "method",
+ returns: true,
+ run: function() {
+ return false;
+ }
+ });
+
+ result = mock.method();
+ Assert.isTrue(result);
+ }
+
+ }));
+
+ //-------------------------------------------------------------------------
+ // Test Case for run expectations
+ //-------------------------------------------------------------------------
+
+ suite.add(new Y.Test.Case({
+
+ name : "Run Tests",
+ groups: ["mock", "common"],
+
+ /*
+ * Test that when run is given it is executed.
+ */
+ "A supplied run function should be invoked": function(){
+ var mock = Y.Test.Mock(),
+ invoked = false;
+
+ Y.Test.Mock.expect(mock, {
+ method: "method",
+ run: function() {
+ invoked = true;
+ }
+ });
+
+ mock.method();
+ Assert.isTrue(invoked);
+ },
+
+ /*
+ * Test that run function return value is used when no 'returns' key is
+ * present.
+ */
+ "A supplied run function's return value should be used.": function(){
+ var mock = Y.Test.Mock(),
+ result;
+
+ Y.Test.Mock.expect(mock, {
+ method: "method",
+ run: function() {
+ return 'invoked';
+ }
+ });
+
+ result = mock.method();
+ Assert.areEqual(result, 'invoked');
+ }
+
+ }));
+
+
Y.Test.Runner.add(suite);
});

0 comments on commit 4670f99

Please sign in to comment.