Permalink
Browse files

Rename mock parameter to stubFn

  • Loading branch information...
1 parent 564e1bc commit 89aadf3aef8bdd519775294ff8a7682e60c7c740 @felixge committed Jun 13, 2010
Showing with 40 additions and 40 deletions.
  1. +4 −4 Readme.md
  2. +18 −18 lib/gently/gently.js
  3. +18 −18 test/simple/test-gently.js
View
@@ -67,15 +67,15 @@ For a full read world example, check out this test case: [test-incoming-form.js]
Creates a new gently instance. It listens to the process `'exit'` event to make sure all expectations have been verified.
-#### gently.expect(obj, method, [[count], mock])
+#### gently.expect(obj, method, [[count], stubFn])
-Creates an expectation for an objects method to be called. You can optionally specify the call `count` you are expecting, as well as `mock` function that will run instead of the original function.
+Creates an expectation for an objects method to be called. You can optionally specify the call `count` you are expecting, as well as `stubFn` function that will run instead of the original function.
Returns a reference to the function that is getting overwritten.
-#### gently.expect([count], mock)
+#### gently.expect([count], stubFn)
-Returns a function that is supposed to be executed `count` times, delegating any calls to the provided `mock` function. Naming your mock closure will help to properly diagnose errors that are being thrown:
+Returns a function that is supposed to be executed `count` times, delegating any calls to the provided `stubFn` function. Naming your stubFn closure will help to properly diagnose errors that are being thrown:
childProcess.exec('ls', gently.expect(function lsCallback(code) {
assert.equal(0, code);
View
@@ -47,41 +47,41 @@ Gently.prototype.hijack = function(realRequire) {
};
};
-Gently.prototype.expect = function(obj, method, count, mock) {
+Gently.prototype.expect = function(obj, method, count, stubFn) {
if (typeof obj != 'function' && typeof obj != 'object') {
throw new Error
( 'Bad 1st argument for gently.expect(), '
+ 'object or function expected, got: '+(typeof obj)
);
} else if (typeof obj == 'function' && (typeof method != 'string')) {
- // expect(mock) interface
- mock = obj;
+ // expect(stubFn) interface
+ stubFn = obj;
obj = null;
method = null;
count = 1;
} else if (typeof method == 'function') {
- // expect(count, mock) interface
+ // expect(count, stubFn) interface
count = obj;
- mock = method;
+ stubFn = method;
obj = null;
method = null;
} else if (typeof count == 'function') {
- // expect(obj, method, mock) interface
- mock = count;
+ // expect(obj, method, stubFn) interface
+ stubFn = count;
count = 1;
} else if (count === undefined) {
// expect(obj, method) interface
count = 1;
}
- var name = this._name(obj, method, mock);
+ var name = this._name(obj, method, stubFn);
while (count-- > 0) {
- this.expectations.push({obj: obj, method: method, mock: mock, name: name});
+ this.expectations.push({obj: obj, method: method, stubFn: stubFn, name: name});
}
var self = this;
function delegate() {
- return self._mock(this, obj, method, name, Array.prototype.slice.call(arguments));
+ return self._stubFn(this, obj, method, name, Array.prototype.slice.call(arguments));
}
if (!obj) {
@@ -95,7 +95,7 @@ Gently.prototype.expect = function(obj, method, count, mock) {
Gently.prototype.restore = function(obj, method) {
if (!obj[method] || !obj[method]._original) {
- throw new Error(this._name(obj, method)+' is not gently mocked');
+ throw new Error(this._name(obj, method)+' is not gently stubbed');
}
obj[method] = obj[method]._original;
};
@@ -115,7 +115,7 @@ Gently.prototype.verify = function(msg) {
);
};
-Gently.prototype._mock = function(self, obj, method, name, args) {
+Gently.prototype._stubFn = function(self, obj, method, name, args) {
var expectation = this.expectations.shift();
if (!expectation) {
throw new Error('Unexpected call to '+name+', no call was expected');
@@ -126,12 +126,12 @@ Gently.prototype._mock = function(self, obj, method, name, args) {
throw new Error('Unexpected call to '+name+', expected call to '+ expectation.name);
}
- if (expectation.mock) {
- return expectation.mock.apply(self, args);
+ if (expectation.stubFn) {
+ return expectation.stubFn.apply(self, args);
}
};
-Gently.prototype._name = function(obj, method, mock) {
+Gently.prototype._name = function(obj, method, stubFn) {
if (obj) {
var objectName = obj.toString();
if (objectName == '[object Object]' && obj.constructor.name) {
@@ -140,9 +140,9 @@ Gently.prototype._name = function(obj, method, mock) {
return (objectName)+'.'+method+'()';
}
- if (mock.name) {
- return mock.name+'()';
+ if (stubFn.name) {
+ return stubFn.name+'()';
}
- return '>> '+mock.toString()+' <<';
+ return '>> '+stubFn.toString()+' <<';
};
View
@@ -35,23 +35,23 @@ test(function expectObjMethod() {
};
var original = OBJ.foo
- , mock = function() {};
+ , stubFn = function() {};
(function testAddOne() {
var original = OBJ.foo;
- assert.strictEqual(gently.expect(OBJ, 'foo', mock), original);
+ assert.strictEqual(gently.expect(OBJ, 'foo', stubFn), original);
assert.equal(gently.expectations.length, 1);
var expectation = gently.expectations[0];
assert.strictEqual(expectation.obj, OBJ);
assert.strictEqual(expectation.method, 'foo');
- assert.strictEqual(expectation.mock, mock);
+ assert.strictEqual(expectation.stubFn, stubFn);
assert.strictEqual(expectation.name, NAME);
assert.strictEqual(OBJ.foo._original, original);
})();
(function testAddTwo() {
- gently.expect(OBJ, 'foo', 2, mock);
+ gently.expect(OBJ, 'foo', 2, stubFn);
assert.equal(gently.expectations.length, 3);
})();
@@ -60,9 +60,9 @@ test(function expectObjMethod() {
assert.equal(gently.expectations.length, 4);
})();
- var mockCalled = 0, SELF = {};
- gently._mock = function(self, obj, method, name, args) {
- mockCalled++;
+ var stubFnCalled = 0, SELF = {};
+ gently._stubFn = function(self, obj, method, name, args) {
+ stubFnCalled++;
assert.strictEqual(self, SELF);
assert.strictEqual(obj, OBJ);
assert.strictEqual(method, 'foo');
@@ -71,7 +71,7 @@ test(function expectObjMethod() {
return 23;
};
assert.equal(OBJ.foo.apply(SELF, [1, 2]), 23);
- assert.equal(mockCalled, 1);
+ assert.equal(stubFnCalled, 1);
});
test(function expectClosure() {
@@ -87,12 +87,12 @@ test(function expectClosure() {
var expectation = gently.expectations[0];
assert.strictEqual(expectation.obj, null);
assert.strictEqual(expectation.method, null);
- assert.strictEqual(expectation.mock, closureFn);
+ assert.strictEqual(expectation.stubFn, closureFn);
assert.strictEqual(expectation.name, NAME);
- var mockCalled = 0, SELF = {};
- gently._mock = function(self, obj, method, name, args) {
- mockCalled++;
+ var stubFnCalled = 0, SELF = {};
+ gently._stubFn = function(self, obj, method, name, args) {
+ stubFnCalled++;
assert.strictEqual(self, SELF);
assert.strictEqual(obj, null);
assert.strictEqual(method, null);
@@ -101,7 +101,7 @@ test(function expectClosure() {
return 23;
};
assert.equal(fn.apply(SELF, [1, 2]), 23);
- assert.equal(mockCalled, 1);
+ assert.equal(stubFnCalled, 1);
});
test(function restore() {
@@ -124,12 +124,12 @@ test(function restore() {
gently.restore(OBJ, 'foo');
assert.ok(false, 'throw needs to happen');
} catch (e) {
- assert.equal(e.message, NAME+' is not gently mocked');
+ assert.equal(e.message, NAME+' is not gently stubbed');
}
})();
});
-test(function mock() {
+test(function _stubFn() {
var OBJ1 = {toString: function() {return '[OBJ 1]'}}
, OBJ2 = {toString: function() {return '[OBJ 2]'}}
, SELF = {};
@@ -139,11 +139,11 @@ test(function mock() {
return x * 2;
});
- assert.equal(gently._mock(SELF, OBJ1, 'foo', 'dummy_name', [5]), 10);
+ assert.equal(gently._stubFn(SELF, OBJ1, 'foo', 'dummy_name', [5]), 10);
(function testNoMoreCallExpected() {
try {
- gently._mock(SELF, OBJ1, 'foo', 'dummy_name', [5]);
+ gently._stubFn(SELF, OBJ1, 'foo', 'dummy_name', [5]);
assert.ok(false, 'throw needs to happen');
} catch (e) {
assert.equal(e.message, 'Unexpected call to dummy_name, no call was expected');
@@ -153,7 +153,7 @@ test(function mock() {
(function testDifferentCallExpected() {
gently.expect(OBJ2, 'bar');
try {
- gently._mock(SELF, OBJ1, 'foo', 'dummy_name', [5]);
+ gently._stubFn(SELF, OBJ1, 'foo', 'dummy_name', [5]);
assert.ok(false, 'throw needs to happen');
} catch (e) {
assert.equal(e.message, 'Unexpected call to dummy_name, expected call to '+gently._name(OBJ2, 'bar'));

0 comments on commit 89aadf3

Please sign in to comment.