Permalink
Browse files

added handler for calling mocked method with wrong params

git-svn-id: file:///Users/jyurek/Backups/repositories/jocha/trunk@8 5317845c-832e-0410-b2c0-db6c5bc7c761
  • Loading branch information...
tfwright
tfwright committed Apr 20, 2007
1 parent ae8a925 commit 79825a11afcaf4a9d79c5c00e450baa8c4a0d9be
Showing with 83 additions and 59 deletions.
  1. +60 −57 jocha.js
  2. +23 −2 test/jocha_test.html
View
117 jocha.js
@@ -1,58 +1,61 @@
- // Thanks http://www.svendtofte.com/code/usefull_prototypes/
- Array.prototype.isEqual = function(arr) {
- if (this.length != arr.length) return false;
- for (var i = 0; i < arr.length; i++) {
- if (this[i].isEqual) { //likely nested array
- if (!this[i].isEqual(arr[i])) return false;
- else continue;
- }
- if (this[i] != arr[i]) return false;
- }
- return true;
- }
-
- Mock = Class.create();
- Mock.prototype = {
- initialize : function() {
- this.expectations = new Array();
- }
- }
- Expectation = Class.create();
- Expectation.prototype = {
- initialize : function(functionName) {
- this.functionName = functionName;
- this.params = [];
- this.invoked = false;
- },
- withParams : function() {
- this.params = $A(arguments);
- return this;
- },
- andReturns : function(return_val) {
- this.return_val = return_val;
- }
- }
-
- Object.extend(Object.prototype, {
- expects : function(functionName) {
- this[functionName] = function() {
- this.methodMocked(functionName, arguments);
- };
- if (!this.mock)
- this.mock = new Mock();
- expectation = new Expectation(functionName);
- this.mock.expectations.push(expectation);
- return expectation;
- },
- verify : function() {
- if (!this.mock.expectations.all(function(e){return e.invoked}))
- return false;
- else
- return true;
- },
- methodMocked : function(functionName, args) {
- expectation = this.mock.expectations.find(function(e){return e.functionName == functionName && e.params.isEqual($A(args))});
- expectation.invoked = true;
- return expectation.return_val;
+// Thanks http://www.svendtofte.com/code/usefull_prototypes/
+Array.prototype.isEqual = function(arr) {
+ if (this.length != arr.length) return false;
+ for (var i = 0; i < arr.length; i++) {
+ if (this[i].isEqual) { //likely nested array
+ if (!this[i].isEqual(arr[i])) return false;
+ else continue;
}
- });
+ if (this[i] != arr[i]) return false;
+ }
+ return true;
+}
+
+Mock = Class.create();
+Mock.prototype = {
+ initialize : function() {
+ this.expectations = new Array();
+ }
+}
+Expectation = Class.create();
+Expectation.prototype = {
+ initialize : function(functionName) {
+ this.functionName = functionName;
+ this.params = [];
+ this.invoked = false;
+ this.return_val = "";
+ },
+ withParams : function() {
+ this.params = $A(arguments);
+ return this;
+ },
+ andReturns : function(returnVal) {
+ this.returnVal = returnVal;
+ }
+}
+
+Object.extend(Object.prototype, {
+ expects : function(functionName) {
+ this[functionName] = function() {
+ this.methodMocked(functionName, arguments);
+ };
+ if (!this.mock)
+ this.mock = new Mock();
+ expectation = new Expectation(functionName);
+ this.mock.expectations.push(expectation);
+ return expectation;
+ },
+ verify : function() {
+ if (this.mock.expectations.all(function(e){return e.invoked}))
+ return true;
+ else
+ return false;
+ },
+ methodMocked : function(functionName, args) {
+ expectation = this.mock.expectations.find(function(e){return e.functionName == functionName && e.params.isEqual($A(args))});
+ if (expectation) {
+ expectation.invoked = true;
+ return expectation.returnVal;
+ }
+ }
+});
View
@@ -5,17 +5,38 @@
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>jocha.js</title>
<script language="JavaScript" type="text/javascript" src="jsunit/app/jsUnitCore.js"></script>
- <!-- Application scripts -->
- <script language="JavaScript" type="text/javascript" src="../chrome/content/js/jocha.js"></script>
+ <script language="JavaScript" type="text/javascript" src="../prototype.js"></script>
+ <script language="JavaScript" type="text/javascript" src="../jocha.js"></script>
</head>
<body>
<script language="JavaScript" type="text/javascript">
function setUp() {
+ obj = new Object();
}
function tearDown() {
}
+ function testExpectsOnAnyObjectOverwritesTheNamedMethodAndVerifiesCalls() {
+ obj.expects("test");
+ assertFalse(obj.verify());
+ obj.test();
+ assertTrue(obj.verify());
+ }
+
+ function testWithParams() {
+ obj.expects("test").withParams("x", "y");
+ obj.test();
+ assertFalse(obj.verify());
+ obj.test("x","y")
+ assertTrue(obj.verify());
+ }
+
+ function testAndReturns() {
+ obj.expects("test").withParams("x", "y").andReturns("pooka");
+ assertEquals("pooka", obj.test("x", "y"))
+ }
+
</script>
</body>
</html>

0 comments on commit 79825a1

Please sign in to comment.