Permalink
Browse files

Message for verify

Also make sure implicit process exit verification is indicated that
way.
  • Loading branch information...
1 parent 9659881 commit 1176dd3d58edceb580d61f935163c7599c1d41db @felixge committed May 21, 2010
Showing with 28 additions and 6 deletions.
  1. +3 −1 Readme.md
  2. +12 −4 lib/gently/gently.js
  3. +13 −1 test/simple/test-gently.js
View
@@ -71,10 +71,12 @@ Creates an expectation for an objects method to be called. You can optionally sp
Restores an object method that has been previously overwritten using `gently.expect()`.
-#### gently.verify()
+#### gently.verify([msg])
Verifies that all expectations of this gently instance have been satisfied. If not called manually, this method is called when the process `'exit'` event is fired.
+If `msg` is given, it will appear in any error that might be thrown.
+
## License
Gently is licensed under the MIT license.
View
@@ -3,10 +3,14 @@ var Gently = exports.Gently = function() {
var self = this;
process.addListener('exit', function() {
- self.verify();
+ self.verify('process exit');
});
};
+Gently.prototype.toString = function() {
+ return '[Gently]';
+};
+
Gently.prototype.expect = function(obj, method, count, mock) {
if (typeof count == 'function') {
mock = count;
@@ -33,7 +37,7 @@ Gently.prototype.restore = function(obj, method) {
obj[method] = obj[method]._original;
};
-Gently.prototype.verify = function() {
+Gently.prototype.verify = function(msg) {
if (!this.expectations.length) {
return;
}
@@ -42,6 +46,10 @@ Gently.prototype.verify = function() {
throw new Error
( 'Expected call to '+expectation.obj.toString()+'.'+expectation.method
+ ' did not happen'
+ + ( (msg)
+ ? ' ('+msg+')'
+ : ''
+ )
);
};
@@ -50,15 +58,15 @@ Gently.prototype._mock = function(self, obj, method, args) {
if (!expectation) {
throw new Error
( 'Unexpected call to '+obj.toString()+'.'+method+'(),'
- +' no call was expected'
+ + ' no call was expected'
);
}
if (expectation.obj !== obj || expectation.method !== method) {
this.expectations.unshift(expectation);
throw new Error
( 'Unexpected call to '+obj.toString()+'.'+method+'(), expected call to '
- +expectation.obj.toString()+'.'+expectation.method+'()'
+ + expectation.obj.toString()+'.'+expectation.method+'()'
);
}
@@ -12,6 +12,10 @@ test(function constructor() {
assert.deepEqual(gently.expectations, []);
});
+test(function toString() {
+ assert.deepEqual(gently.toString(), '[Gently]');
+});
+
test(function expect() {
var OBJ = {};
OBJ.foo = function(x) {
@@ -130,12 +134,20 @@ test(function verify() {
} catch (e) {
assert.equal(e.message, 'Expected call to [OBJ].foo did not happen');
}
+
+ try {
+ gently.verify('foo');
+ assert.ok(false, 'throw needs to happen');
+ } catch (e) {
+ assert.equal(e.message, 'Expected call to [OBJ].foo did not happen (foo)');
+ }
});
test(function processExit() {
var verifyCalled = 0;
- gently.verify = function() {
+ gently.verify = function(msg) {
verifyCalled++;
+ assert.equal(msg, 'process exit');
};
process.emit('exit');

0 comments on commit 1176dd3

Please sign in to comment.