mock.html revision 8e2f5a54575e4da16c524b6c45cba19a4ad00070
0N/A<html>
0N/A<head>
3516N/A<title>mock tests</title>
3516N/A<link type="text/css" rel="stylesheet" href="/build/logreader/assets/skins/sam/logreader.css" />
3516N/A<script type="text/javascript" src="/build/yui/yui.js"></script>
3516N/A</head>
3516N/A<body class="yui-skin-sam">
3516N/A <h1>Mock Tests</h1>
3516N/A <div id="c"></div>
0N/A<script type="text/javascript">
0N/A
0N/AYUI({
0N/A base: '/build/',
0N/A filter: "debug",
0N/A logInclude: { TestRunner: true }
0N/A}).use('test', 'console', function (Y) {
5056N/A
0N/A Y.namespace("Tests");
5056N/A
5056N/A Y.Tests.Mock = (function(){
5056N/A
0N/A var Assert = Y.Assert,
0N/A ObjectAssert = Y.ObjectAssert;
0N/A
3516N/A //-------------------------------------------------------------------------
0N/A // Base Test Suite
0N/A //-------------------------------------------------------------------------
3516N/A
3516N/A var suite = new Y.Test.Suite("Mock Tests");
0N/A
0N/A //-------------------------------------------------------------------------
4986N/A // Test Case for call count
4986N/A //-------------------------------------------------------------------------
4986N/A
4986N/A suite.add(new Y.Test.Case({
0N/A
0N/A name : "Call Count Tests",
5056N/A
5056N/A _should: {
0N/A fail: {
4986N/A "Call count should default to 1 and fail": 1,
0N/A "Call count set to 1 should fail when method isn't called": 1,
3516N/A "Call count set to 1 should fail when method is called twice": 1,
5056N/A "Call count set to 0 should fail when method is called once": 1
4986N/A }
5056N/A },
4986N/A
0N/A /*
5056N/A * Tests that leaving off callCount results in a callCount of 1, so
5056N/A * calling the mock method once should make the test pass.
4986N/A */
4986N/A "Call count should default to 1 and pass": function(){
4986N/A
4986N/A var mock = Y.Mock();
5056N/A Y.Mock.expect(mock, {
4986N/A method: "method"
5056N/A });
4986N/A
5056N/A mock.method();
4986N/A Y.Mock.verify(mock);
5056N/A },
4986N/A
0N/A /*
0N/A * Tests that leaving off callCount results in a callCount of 1, so
4986N/A * not calling the mock method once should make the test fail.
4986N/A */
5056N/A "Call count should default to 1 and fail": function(){
0N/A
3516N/A var mock = Y.Mock();
3516N/A Y.Mock.expect(mock, {
0N/A method: "method"
0N/A });
5056N/A
0N/A Y.Mock.verify(mock);
0N/A },
0N/A
0N/A /*
0N/A * Tests that setting callCount to 1 and
0N/A * calling the mock method once should make the test pass.
0N/A */
0N/A "Call count set to 1 should pass when method is called once": function(){
0N/A
0N/A var mock = Y.Mock();
0N/A Y.Mock.expect(mock, {
0N/A method: "method",
0N/A callCount: 1
0N/A });
0N/A
0N/A mock.method();
0N/A Y.Mock.verify(mock);
0N/A },
0N/A
0N/A /*
0N/A * Tests that setting callCount to 1 and not
0N/A * calling the mock method once should make the test fail.
0N/A */
0N/A "Call count set to 1 should fail when method isn't called": function(){
0N/A
0N/A var mock = Y.Mock();
0N/A Y.Mock.expect(mock, {
0N/A method: "method",
0N/A callCount: 1
0N/A });
0N/A
0N/A Y.Mock.verify(mock);
0N/A },
0N/A
0N/A /*
0N/A * Tests that setting callCount to 1 and not
0N/A * calling the mock method twice should make the test fail.
0N/A */
0N/A "Call count set to 1 should fail when method is called twice": function(){
0N/A
0N/A var mock = Y.Mock();
0N/A Y.Mock.expect(mock, {
0N/A method: "method",
0N/A callCount: 1
0N/A });
0N/A
0N/A mock.method();
0N/A mock.method();
0N/A
0N/A Y.Mock.verify(mock);
0N/A },
0N/A
0N/A /*
3516N/A * Tests that setting callCount to 0 and
0N/A * calling the mock method once should make the test fail.
0N/A */
0N/A "Call count set to 0 should fail when method is called once": function(){
0N/A
0N/A var mock = Y.Mock();
0N/A Y.Mock.expect(mock, {
0N/A method: "method",
0N/A callCount: 0
0N/A });
3516N/A
3516N/A mock.method();
3516N/A Y.Mock.verify(mock);
0N/A },
0N/A
3516N/A /*
0N/A * Tests that setting callCount to 0 and not
0N/A * calling the mock method once should make the test pass.
0N/A */
0N/A "Call count set to 0 should pass when method isn't called": function(){
0N/A
0N/A var mock = Y.Mock();
3516N/A Y.Mock.expect(mock, {
0N/A method: "method",
0N/A callCount: 0
3516N/A });
3516N/A
0N/A Y.Mock.verify(mock);
3516N/A }
3516N/A
0N/A }));
0N/A
0N/A //-------------------------------------------------------------------------
3516N/A // Test Case for arguments
3516N/A //-------------------------------------------------------------------------
0N/A
3516N/A suite.add(new Y.Test.Case({
0N/A
3516N/A name : "Arguments Tests",
3516N/A
0N/A _should: {
0N/A fail: {
0N/A "Passing an incorrect number of arguments should make the test fail": 1,
0N/A "Passing an inexact argument should make the test fail" : 1,
0N/A
0N/A "Passing a number to an Boolean argument should make the test fail": 1,
0N/A "Passing a string to an Boolean argument should make the test fail": 1,
0N/A "Passing a object to an Boolean argument should make the test fail": 1,
0N/A "Passing a function to an Boolean argument should make the test fail": 1,
3516N/A "Passing a null to an Boolean argument should make the test fail": 1,
0N/A
5056N/A "Passing a number to an String argument should make the test fail": 1,
0N/A "Passing a boolean to an String argument should make the test fail": 1,
0N/A "Passing a object to an String argument should make the test fail": 1,
3516N/A "Passing a function to an String argument should make the test fail": 1,
3516N/A "Passing a null to an String argument should make the test fail": 1,
"Passing a string to an Number argument should make the test fail": 1,
"Passing a boolean to an Number argument should make the test fail": 1,
"Passing a object to an Number argument should make the test fail": 1,
"Passing a function to an Number argument should make the test fail": 1,
"Passing a null to an Number argument should make the test fail": 1,
"Passing a string to an Object argument should make the test fail": 1,
"Passing a boolean to an Object argument should make the test fail": 1,
"Passing a number to an Object argument should make the test fail": 1,
"Passing a null to an Object argument should make the test fail": 1,
"Passing a string to an Function argument should make the test fail": 1,
"Passing a boolean to an Function argument should make the test fail": 1,
"Passing a number to an Function argument should make the test fail": 1,
"Passing a object to an Function argument should make the test fail": 1,
"Passing a null to an Function argument should make the test fail": 1
}
},
/*
* Tests that when the number of arguments is verified, the test passes.
*/
"Passing correct number of arguments should make the test pass": function(){
var mock = Y.Mock();
Y.Mock.expect(mock, {
method: "method",
args: [ Y.Mock.Value.Any ]
});
mock.method(1);
Y.Mock.verify(mock);
},
/*
* Tests that when the number of arguments is not verified, the test fails.
*/
"Passing an incorrect number of arguments should make the test fail": function(){
var mock = Y.Mock();
Y.Mock.expect(mock, {
method: "method",
args: [ Y.Mock.Value.Any ]
});
mock.method(1, 2);
Y.Mock.verify(mock);
},
/*
* Tests that passing the exactly specified argument causes the test to pass.
*/
"Passing the exact argument should make the test pass": function(){
var arg = {};
var mock = Y.Mock();
Y.Mock.expect(mock, {
method: "method",
args: [ arg ]
});
mock.method(arg);
Y.Mock.verify(mock);
},
/*
* Tests that passing an argument that isn't exactly specified argument causes the test to fail.
*/
"Passing an inexact argument should make the test fail": function(){
var arg = {};
var mock = Y.Mock();
Y.Mock.expect(mock, {
method: "method",
args: [ arg ]
});
mock.method({});
Y.Mock.verify(mock);
},
//Y.Mock.Value.Any tests --------------------------------------
/*
* Tests that passing a number to an argument specified as Y.Mock.Value.Any
* results cause the test to pass.
*/
"Passing a number to an Any argument should make the test pass": function(){
var mock = Y.Mock();
Y.Mock.expect(mock, {
method: "method",
args: [ Y.Mock.Value.Any ]
});
mock.method(1);
Y.Mock.verify(mock);
},
/*
* Tests that passing a boolean to an argument specified as Y.Mock.Value.Any
* results cause the test to pass.
*/
"Passing a boolean to an Any argument should make the test pass": function(){
var mock = Y.Mock();
Y.Mock.expect(mock, {
method: "method",
args: [ Y.Mock.Value.Any ]
});
mock.method(true);
Y.Mock.verify(mock);
},
/*
* Tests that passing a string to an argument specified as Y.Mock.Value.Any
* results cause the test to pass.
*/
"Passing a string to an Any argument should make the test pass": function(){
var mock = Y.Mock();
Y.Mock.expect(mock, {
method: "method",
args: [ Y.Mock.Value.Any ]
});
mock.method("");
Y.Mock.verify(mock);
},
/*
* Tests that passing an object to an argument specified as Y.Mock.Value.Any
* results cause the test to pass.
*/
"Passing a object to an Any argument should make the test pass": function(){
var mock = Y.Mock();
Y.Mock.expect(mock, {
method: "method",
args: [ Y.Mock.Value.Any ]
});
mock.method({});
Y.Mock.verify(mock);
},
/*
* Tests that passing a function to an argument specified as Y.Mock.Value.Any
* results cause the test to pass.
*/
"Passing a function to an Any argument should make the test pass": function(){
var mock = Y.Mock();
Y.Mock.expect(mock, {
method: "method",
args: [ Y.Mock.Value.Any ]
});
mock.method(function(){});
Y.Mock.verify(mock);
},
/*
* Tests that passing a null to an argument specified as Y.Mock.Value.Any
* results cause the test to pass.
*/
"Passing a null to an Any argument should make the test pass": function(){
var mock = Y.Mock();
Y.Mock.expect(mock, {
method: "method",
args: [ Y.Mock.Value.Any ]
});
mock.method(null);
Y.Mock.verify(mock);
},
//Y.Mock.Value.Boolean tests --------------------------------------
/*
* Tests that passing a number to an argument specified as Y.Mock.Value.Boolean
* results cause the test to fail.
*/
"Passing a number to an Boolean argument should make the test fail": function(){
var mock = Y.Mock();
Y.Mock.expect(mock, {
method: "method",
args: [ Y.Mock.Value.Boolean ]
});
mock.method(1);
Y.Mock.verify(mock);
},
/*
* Tests that passing a boolean to an argument specified as Y.Mock.Value.Boolean
* results cause the test to pass.
*/
"Passing a boolean to an Boolean argument should make the test pass": function(){
var mock = Y.Mock();
Y.Mock.expect(mock, {
method: "method",
args: [ Y.Mock.Value.Boolean ]
});
mock.method(true);
Y.Mock.verify(mock);
},
/*
* Tests that passing a string to an argument specified as Y.Mock.Value.Boolean
* results cause the test to fail.
*/
"Passing a string to an Boolean argument should make the test fail": function(){
var mock = Y.Mock();
Y.Mock.expect(mock, {
method: "method",
args: [ Y.Mock.Value.Boolean ]
});
mock.method("");
Y.Mock.verify(mock);
},
/*
* Tests that passing an object to an argument specified as Y.Mock.Value.Boolean
* results cause the test to fail.
*/
"Passing a object to an Boolean argument should make the test fail": function(){
var mock = Y.Mock();
Y.Mock.expect(mock, {
method: "method",
args: [ Y.Mock.Value.Boolean ]
});
mock.method({});
Y.Mock.verify(mock);
},
/*
* Tests that passing a function to an argument specified as Y.Mock.Value.Boolean
* results cause the test to fail.
*/
"Passing a function to an Boolean argument should make the test fail": function(){
var mock = Y.Mock();
Y.Mock.expect(mock, {
method: "method",
args: [ Y.Mock.Value.Boolean ]
});
mock.method(function(){});
Y.Mock.verify(mock);
},
/*
* Tests that passing a null to an argument specified as Y.Mock.Value.Boolean
* results cause the test to fail.
*/
"Passing a null to an Boolean argument should make the test fail": function(){
var mock = Y.Mock();
Y.Mock.expect(mock, {
method: "method",
args: [ Y.Mock.Value.Boolean ]
});
mock.method(null);
Y.Mock.verify(mock);
},
//Y.Mock.Value.String tests --------------------------------------
/*
* Tests that passing a number to an argument specified as Y.Mock.Value.String
* results cause the test to fail.
*/
"Passing a number to an String argument should make the test fail": function(){
var mock = Y.Mock();
Y.Mock.expect(mock, {
method: "method",
args: [ Y.Mock.Value.String ]
});
mock.method(1);
Y.Mock.verify(mock);
},
/*
* Tests that passing a boolean to an argument specified as Y.Mock.Value.String
* results cause the test to fail.
*/
"Passing a boolean to an String argument should make the test fail": function(){
var mock = Y.Mock();
Y.Mock.expect(mock, {
method: "method",
args: [ Y.Mock.Value.String ]
});
mock.method(true);
Y.Mock.verify(mock);
},
/*
* Tests that passing a string to an argument specified as Y.Mock.Value.String
* results cause the test to pass.
*/
"Passing a string to an String argument should make the test pass": function(){
var mock = Y.Mock();
Y.Mock.expect(mock, {
method: "method",
args: [ Y.Mock.Value.String ]
});
mock.method("");
Y.Mock.verify(mock);
},
/*
* Tests that passing an object to an argument specified as Y.Mock.Value.String
* results cause the test to fail.
*/
"Passing a object to an String argument should make the test fail": function(){
var mock = Y.Mock();
Y.Mock.expect(mock, {
method: "method",
args: [ Y.Mock.Value.String ]
});
mock.method({});
Y.Mock.verify(mock);
},
/*
* Tests that passing a function to an argument specified as Y.Mock.Value.String
* results cause the test to fail.
*/
"Passing a function to an String argument should make the test fail": function(){
var mock = Y.Mock();
Y.Mock.expect(mock, {
method: "method",
args: [ Y.Mock.Value.String ]
});
mock.method(function(){});
Y.Mock.verify(mock);
},
/*
* Tests that passing a null to an argument specified as Y.Mock.Value.String
* results cause the test to fail.
*/
"Passing a null to an String argument should make the test fail": function(){
var mock = Y.Mock();
Y.Mock.expect(mock, {
method: "method",
args: [ Y.Mock.Value.String ]
});
mock.method(null);
Y.Mock.verify(mock);
},
//Y.Mock.Value.Number tests --------------------------------------
/*
* Tests that passing a number to an argument specified as Y.Mock.Value.Number
* results cause the test to pass.
*/
"Passing a number to an Number argument should make the test pass": function(){
var mock = Y.Mock();
Y.Mock.expect(mock, {
method: "method",
args: [ Y.Mock.Value.Number ]
});
mock.method(1);
Y.Mock.verify(mock);
},
/*
* Tests that passing a boolean to an argument specified as Y.Mock.Value.Number
* results cause the test to fail.
*/
"Passing a boolean to an Number argument should make the test fail": function(){
var mock = Y.Mock();
Y.Mock.expect(mock, {
method: "method",
args: [ Y.Mock.Value.Number ]
});
mock.method(true);
Y.Mock.verify(mock);
},
/*
* Tests that passing a string to an argument specified as Y.Mock.Value.Number
* results cause the test to fail.
*/
"Passing a string to an Number argument should make the test fail": function(){
var mock = Y.Mock();
Y.Mock.expect(mock, {
method: "method",
args: [ Y.Mock.Value.Number ]
});
mock.method("");
Y.Mock.verify(mock);
},
/*
* Tests that passing an object to an argument specified as Y.Mock.Value.Number
* results cause the test to fail.
*/
"Passing a object to an Number argument should make the test fail": function(){
var mock = Y.Mock();
Y.Mock.expect(mock, {
method: "method",
args: [ Y.Mock.Value.Number ]
});
mock.method({});
Y.Mock.verify(mock);
},
/*
* Tests that passing a function to an argument specified as Y.Mock.Value.Number
* results cause the test to fail.
*/
"Passing a function to an Number argument should make the test fail": function(){
var mock = Y.Mock();
Y.Mock.expect(mock, {
method: "method",
args: [ Y.Mock.Value.Number ]
});
mock.method(function(){});
Y.Mock.verify(mock);
},
/*
* Tests that passing a null to an argument specified as Y.Mock.Value.Number
* results cause the test to fail.
*/
"Passing a null to an Number argument should make the test fail": function(){
var mock = Y.Mock();
Y.Mock.expect(mock, {
method: "method",
args: [ Y.Mock.Value.Number ]
});
mock.method(null);
Y.Mock.verify(mock);
},
//Y.Mock.Value.Function tests --------------------------------------
/*
* Tests that passing a number to an argument specified as Y.Mock.Value.Function
* results cause the test to fail.
*/
"Passing a number to an Function argument should make the test fail": function(){
var mock = Y.Mock();
Y.Mock.expect(mock, {
method: "method",
args: [ Y.Mock.Value.Function ]
});
mock.method(1);
Y.Mock.verify(mock);
},
/*
* Tests that passing a boolean to an argument specified as Y.Mock.Value.Function
* results cause the test to fail.
*/
"Passing a boolean to an Function argument should make the test fail": function(){
var mock = Y.Mock();
Y.Mock.expect(mock, {
method: "method",
args: [ Y.Mock.Value.Function ]
});
mock.method(true);
Y.Mock.verify(mock);
},
/*
* Tests that passing a string to an argument specified as Y.Mock.Value.Function
* results cause the test to fail.
*/
"Passing a string to an Function argument should make the test fail": function(){
var mock = Y.Mock();
Y.Mock.expect(mock, {
method: "method",
args: [ Y.Mock.Value.Function ]
});
mock.method("");
Y.Mock.verify(mock);
},
/*
* Tests that passing an object to an argument specified as Y.Mock.Value.Function
* results cause the test to fail.
*/
"Passing a object to an Function argument should make the test fail": function(){
var mock = Y.Mock();
Y.Mock.expect(mock, {
method: "method",
args: [ Y.Mock.Value.Function ]
});
mock.method({});
Y.Mock.verify(mock);
},
/*
* Tests that passing a function to an argument specified as Y.Mock.Value.Function
* results cause the test to pass.
*/
"Passing a function to an Function argument should make the test pass": function(){
var mock = Y.Mock();
Y.Mock.expect(mock, {
method: "method",
args: [ Y.Mock.Value.Function ]
});
mock.method(function(){});
Y.Mock.verify(mock);
},
/*
* Tests that passing a null to an argument specified as Y.Mock.Value.Function
* results cause the test to fail.
*/
"Passing a null to an Function argument should make the test fail": function(){
var mock = Y.Mock();
Y.Mock.expect(mock, {
method: "method",
args: [ Y.Mock.Value.Function ]
});
mock.method(null);
Y.Mock.verify(mock);
},
//Y.Mock.Value.Object tests --------------------------------------
/*
* Tests that passing a number to an argument specified as Y.Mock.Value.Object
* results cause the test to fail.
*/
"Passing a number to an Object argument should make the test fail": function(){
var mock = Y.Mock();
Y.Mock.expect(mock, {
method: "method",
args: [ Y.Mock.Value.Object ]
});
mock.method(1);
Y.Mock.verify(mock);
},
/*
* Tests that passing a boolean to an argument specified as Y.Mock.Value.Object
* results cause the test to fail.
*/
"Passing a boolean to an Object argument should make the test fail": function(){
var mock = Y.Mock();
Y.Mock.expect(mock, {
method: "method",
args: [ Y.Mock.Value.Object ]
});
mock.method(true);
Y.Mock.verify(mock);
},
/*
* Tests that passing a string to an argument specified as Y.Mock.Value.Object
* results cause the test to fail.
*/
"Passing a string to an Object argument should make the test fail": function(){
var mock = Y.Mock();
Y.Mock.expect(mock, {
method: "method",
args: [ Y.Mock.Value.Object ]
});
mock.method("");
Y.Mock.verify(mock);
},
/*
* Tests that passing an object to an argument specified as Y.Mock.Value.Object
* results cause the test to pass.
*/
"Passing a object to an Object argument should make the test pass": function(){
var mock = Y.Mock();
Y.Mock.expect(mock, {
method: "method",
args: [ Y.Mock.Value.Object ]
});
mock.method({});
Y.Mock.verify(mock);
},
/*
* Tests that passing a function to an argument specified as Y.Mock.Value.Object
* results cause the test to pass.
*/
"Passing a function to an Object argument should make the test pass": function(){
var mock = Y.Mock();
Y.Mock.expect(mock, {
method: "method",
args: [ Y.Mock.Value.Object ]
});
mock.method(function(){});
Y.Mock.verify(mock);
},
/*
* Tests that passing a null to an argument specified as Y.Mock.Value.Object
* results cause the test to fail.
*/
"Passing a null to an Object argument should make the test fail": function(){
var mock = Y.Mock();
Y.Mock.expect(mock, {
method: "method",
args: [ Y.Mock.Value.Object ]
});
mock.method(null);
Y.Mock.verify(mock);
}
}));
//-------------------------------------------------------------------------
// Test Case for asynchronous mock calls
//-------------------------------------------------------------------------
suite.add(new Y.Test.Case({
name : "Asynchronous Tests",
_should: {
fail: {
"A mock method called asynchronously shouldn't cause an error": 1
}
},
/*
* Tests that when a mock method is called asynchronously, either via
* timeout or XHR callback, that its error is properly handled and
* the failure is logged to the test.
*/
"A mock method called asynchronously shouldn't cause an error": function(){
var mock = Y.Mock();
Y.Mock.expect(mock, {
method: "method",
args: [ Y.Mock.Value.String ]
});
setTimeout(function(){
mock.method(null);
}, 250);
this.wait(function(){
Y.Mock.verify(mock);
}, 500);
}
}));
//return it
return suite;
})();
var r = new Y.Console({
verbose : true,
//consoleLimit : 10,
newestOnTop : false
});
r.render('#c');
//add to the testrunner and run
Y.Test.Runner.add(Y.Tests.Mock);
Y.Test.Runner.run();
/*if (parent && parent != window) {
YAHOO.tool.TestManager.load();
} else {
YAHOO.tool.TestRunner.run();
}*/
});
</script>
</body>
</html>