Skip to content
Browse files

Improved test readability.

  • Loading branch information...
1 parent e59319e commit 3d58dcb0128daff22dcb36b859ccb086e5a9da00 lollo committed Dec 6, 2012
View
56 test/controller.js
@@ -1,38 +1,38 @@
/**
- * Controller tests
+ * Module dependencies
*/
-var assert = require('assert');
+var should = require('should');
var util = require('util');
var path = require('path');
-var sinon = require('sinon');
var Controller = require('../lib/controller');
-var appDir = path.join(__dirname, 'test', 'testdir');
-var app = {
- get: function(key) {
- switch(key) {
- case 'appDir':
- return appDir;
- break;
- }
- }
-}
-
-var controllerPath = '/path';
-
-var req = {}
-
-var next = function() {}
+/**
+ * Tests
+ */
describe('Controller', function() {
+ var appDir = path.join(__dirname, 'test', 'testdir');
+ var app = {
+ get: function(key) {
+ switch(key) {
+ case 'appDir':
+ return appDir;
+ break;
+ }
+ }
+ }
+ var controllerPath = '/path';
+ var req = {}
+ var next = function() {}
+
describe('.render()', function() {
it('should call res.render with 3 arguments', function(done) {
var res = {
render: function(view, locals, callback) {
- assert.strictEqual(view, 'view');
- assert.strictEqual(locals.foo, 'bar');
- assert.strictEqual((typeof callback), 'function');
+ view.should.equal('view');
+ locals['foo'].should.equal('bar');
+ callback.should.be.a('function');
done();
}
}
@@ -44,9 +44,9 @@ describe('Controller', function() {
it('should shift arguments one left when there are 2', function(done) {
var res = {
render: function(view, locals, callback) {
- assert.strictEqual(view, 'path');
- assert.strictEqual(locals.foo, 'bar');
- assert.strictEqual((typeof callback), 'function');
+ view.should.equal('path');
+ locals['foo'].should.equal('bar');
+ callback.should.be.a('function');
done();
}
}
@@ -58,9 +58,9 @@ describe('Controller', function() {
it('should shift arguments 2 left when there is 1', function(done) {
var res = {
render: function(view, locals, callback) {
- assert.strictEqual(view, 'path');
- assert.strictEqual((typeof locals), 'object');
- assert.strictEqual((typeof callback), 'function');
+ view.should.equal('path');
+ locals.should.be.a('object');
+ callback.should.be.a('function');
done();
}
}
View
63 test/controllers.js
@@ -1,48 +1,51 @@
/**
- * Controllers tests
+ * Module dependencies
*/
-var assert = require('assert');
+var should = require('should');
var path = require('path');
var Controllers = require('../lib/controllers');
+var util = require('util');
-var appDir = path.join(__dirname, 'testdir');
-var app = {
- get: function(key) {
- switch(key) {
- case 'appDir':
- return appDir;
- break;
- }
- }
-}
-var controllers = new Controllers(app);
+/**
+ * Tests
+ */
describe('Controllers', function() {
+ var appDir = path.join(__dirname, 'testdir');
+ var app = {
+ get: function(key) {
+ switch(key) {
+ case 'appDir':
+ return appDir;
+ break;
+ }
+ }
+ }
+ var controllers = new Controllers(app);
+
describe('.buildList()', function() {
it('should build list correctly', function(done) {
require.extensions['.controller.js'] = require.extensions['.js'];
controllers.buildList(function(list) {
- var dirfile = {
- method: 'method',
- method2: 'method2'
- }
-
- var dir2dir3file3 = {
- nil: 'nil',
- method4: 'method4'
- }
-
- var with_extension = {
- method: 'method'
+ var expected = {
+ '/dir/subdir/controller': {
+ foo: 'foo'
+ },
+ '/dir/file': {
+ foo: 'foo',
+ bar: 'bar'
+ },
+ '/dir': {
+ bar: 'bar'
+ },
+ '/with_extension': {
+ baz: 'baz'
+ }
}
- assert.deepEqual(list['/dir'], {file: 'file'});
- assert.deepEqual(list['/dir/file'], dirfile);
- assert.deepEqual(list['/dir/file/method'], {foo: 'foo'});
- assert.deepEqual(list['/dir2/dir3/file3'], dir2dir3file3);
- assert.deepEqual(list['/with_extension'], with_extension);
+ should.deepEqual(list, expected);
delete require.extensions['.controller.js'];
done();
View
103 test/factory.js
@@ -1,148 +1,133 @@
/**
- * Factory tests
+ * Module dependencies
*/
-var assert = require('assert');
+var should = require('should');
var util = require('util');
var path = require('path');
var Factory = require('../lib/factory');
-var appDir = path.join(__dirname, 'testdir');
-
-var app = {
- get: function(key) {
- switch(key) {
- case 'appDir':
- return appDir
- break;
- }
- },
- settings: {key: 'value'}
-}
+/**
+ * Tests
+ */
describe('Factory', function() {
+ var appDir = path.join(__dirname, 'testdir');
+ var app = {
+ get: function(key) {
+ switch(key) {
+ case 'appDir':
+ return appDir
+ break;
+ }
+ },
+ settings: {key: 'value'}
+ }
+
+ beforeEach(function() {
+ this.factory = new Factory(app);
+ });
+
describe('.getModel()', function() {
- it('should call a function exported in the model', function(done) {
- var factory = new Factory(app);
-
- factory.getModel('foo').getFoo(function(value) {
- assert.strictEqual(value, 'foo');
+ it('should call a function exported in the model', function(done) {
+ this.factory.getModel('foo').getFoo(function(value) {
+ value.should.equal('foo');
done();
});
});
});
it('should call a model prototype function', function(done) {
- var factory = new Factory(app);
-
- var Sum = factory.getModel('sum');
+ var Sum = this.factory.getModel('sum');
var sum = new Sum(1, 2);
sum.get(function(value) {
- assert.strictEqual(value, 3);
+ value.should.equal(3);
done();
});
});
describe('.getService()', function() {
it('should call a function exported in the service', function(done) {
- var factory = new Factory(app);
-
- factory.getService('foo').getFoo(function(value) {
- assert.strictEqual(value, 'foo');
+ this.factory.getService('foo').getFoo(function(value) {
+ value.should.equal('foo');
done();
});
});
it('should call a service prototype function', function(done) {
- var factory = new Factory(app);
-
- var S = factory.getService('sum')
+ var S = this.factory.getService('sum')
var s = new S(2, 3);
s.get(function(value) {
- assert.strictEqual(value, 5);
+ value.should.equal(5);
done();
});
});
});
describe('service.service()', function() {
it('should call a service from the exported function of a service', function(done) {
- var factory = new Factory(app);
-
- factory.getService('foo').getBaz(function(value) {
- assert.strictEqual(value, 'baz');
+ this.factory.getService('foo').getBaz(function(value) {
+ value.should.equal('baz');
done();
});
});
it('should call a service from a service prototype function', function(done) {
- var factory = new Factory(app);
-
- var S = factory.getService('sum');
+ var S = this.factory.getService('sum');
var s = new S(2, 3);
s.getBaz(function(value) {
- assert.strictEqual(value, 'baz');
+ value.should.equal('baz');
done();
});
});
});
describe('service.construct()', function() {
it('should construct service', function(done) {
- var factory = new Factory(app);
-
- factory.getService('sum')
+ this.factory.getService('sum')
.construct(2, 3)
.get(function(value) {
- assert.strictEqual(value, 5);
+ value.should.equal(5);
done();
});
});
});
describe('service.settings', function() {
it('should read setting from service prototype function', function(done) {
- var factory = new Factory(app);
-
- var Sum = factory.getService('sum');
+ var Sum = this.factory.getService('sum');
var sum = new Sum(1, 2);
sum.getSetting(function(value) {
- assert.strictEqual(value, 'value');
+ value.should.equal('value');
done();
});
});
});
describe('service.settings', function() {
it('should read setting from an exported service function', function(done) {
- var factory = new Factory(app);
-
- factory.getService('foo').getSetting(function(value) {
- assert.strictEqual(value, 'value');
+ this.factory.getService('foo').getSetting(function(value) {
+ value.should.equal('value');
done();
});
});
});
describe('model.settings', function() {
it('should read setting from model prototype function', function(done) {
- var factory = new Factory(app);
-
- var Sum = factory.getModel('sum');
+ var Sum = this.factory.getModel('sum');
var sum = new Sum(1, 2);
sum.getSetting(function(value) {
- assert.strictEqual(value, 'value');
+ value.should.equal('value');
done();
});
});
});
describe('model.settings', function() {
it('should read setting from an exported model function', function(done) {
- var factory = new Factory(app);
-
- factory.getModel('foo').getSetting(function(value) {
- assert.strictEqual(value, 'value');
+ this.factory.getModel('foo').getSetting(function(value) {
+ value.should.equal('value');
done();
});
});
View
341 test/router.js
@@ -1,366 +1,369 @@
/**
- * Router tests
- *
- * TODO They need to be completely rewritten
+ * Module dependencies
*/
-var assert = require('assert');
+var should = require('should');
var sinon = require('sinon');
var path = require('path');
var Router = require('../lib/router');
var util = require('util');
-var appDir = path.join(__dirname, 'test', 'testdir');
+/**
+ * Tests
+ */
-var app = {
- get: function(key) {
- switch(key) {
- case 'appDir':
- return appDir;
- break;
-
- case 'defaultController':
- return 'home';
- break;
+describe('Router', function() {
+ var appDir = path.join(__dirname, 'test', 'testdir');
+
+ var app = {
+ get: function(key) {
+ switch(key) {
+ case 'appDir':
+ return appDir;
+ break;
+
+ case 'defaultController':
+ return 'home';
+ break;
+ }
}
}
-}
-var controllers = {
- '/home': {
- read: function() {
- this.res.send();
- },
- foo: function() {
- this.res.send();
- },
- baz: function(arg1) {
- this.res.send();
- },
- camelCase: function() {
- this.res.send();
- }
- },
- '/home/baz': {
- bar: function() {
- this.res.send();
- }
- },
- '/foo/foo2': {
- bar: function() {
- this.res.send();
- }
- },
- '/crud': {
- read: function(id) {
- this.res.send();
- },
- create: function() {
- this.res.send();
- },
- update: function(id) {
- this.res.send();
- },
- del: function(id) {
- this.res.send();
- },
- add: function(id) {
- this.res.send();
+ var controllers = {
+ '/home': {
+ read: function() {
+ this.res.send();
+ },
+ foo: function() {
+ this.res.send();
+ },
+ baz: function(arg1) {
+ this.res.send();
+ },
+ camelCase: function() {
+ this.res.send();
+ }
},
- edit: function(id) {
- this.res.send();
- }
- },
- '/noncrud': {
- resource: false,
- read: function(id) {
- this.res.send();
+ '/home/baz': {
+ bar: function() {
+ this.res.send();
+ }
},
- create: function() {
- this.res.send();
+ '/foo/foo2': {
+ bar: function() {
+ this.res.send();
+ }
},
- update: function(id) {
- this.res.send();
+ '/crud': {
+ read: function(id) {
+ this.res.send();
+ },
+ create: function() {
+ this.res.send();
+ },
+ update: function(id) {
+ this.res.send();
+ },
+ del: function(id) {
+ this.res.send();
+ },
+ add: function(id) {
+ this.res.send();
+ },
+ edit: function(id) {
+ this.res.send();
+ }
},
- del: function(id) {
- this.res.send();
+ '/noncrud': {
+ resource: false,
+ read: function(id) {
+ this.res.send();
+ },
+ create: function() {
+ this.res.send();
+ },
+ update: function(id) {
+ this.res.send();
+ },
+ del: function(id) {
+ this.res.send();
+ }
}
}
-}
-
-var req = {}
-
-describe('Router', function() {
+
+ beforeEach(function() {
+ this.req = this.res = {}
+ this.router = new Router(app, controllers);
+ });
+
describe('.handle()', function() {
it('should call /home.foo()', function(done) {
- req.path = '/home/foo';
+ this.req.path = '/home/foo';
var spy = sinon.spy(controllers['/home'], 'foo');
- var res = {
+ this.res = {
send: function() {
- assert.ok(spy.called);
+ spy.called.should.be.ok;
+
spy.restore();
done();
},
status: function() {}
}
- var router = new Router(app, controllers);
- router.handle(req, res);
+ this.router.handle(this.req, this.res);
});
it('should call /foo/foo2.bar()', function(done) {
- req.path = '/foo/foo2/bar';
+ this.req.path = '/foo/foo2/bar';
var spy = sinon.spy(controllers['/foo/foo2'], 'bar');
- var res = {
+ this.res = {
send: function() {
- assert.ok(spy.called);
+ spy.called.should.be.ok;
+
spy.restore();
done();
},
status: function() {}
}
- var router = new Router(app, controllers);
- router.handle(req, res);
+ this.router.handle(this.req, this.res);
});
it('should call /home.read()', function(done) {
- req.path = '/home';
- req.method = 'GET';
+ this.req.path = '/home';
+ this.req.method = 'GET';
var spy = sinon.spy(controllers['/home'], 'read');
- var res = {
+ this.res = {
send: function() {
- assert.ok(spy.called);
+ spy.called.should.be.ok;
+
spy.restore();
done();
},
status: function() {}
}
- var router = new Router(app, controllers);
- router.handle(req, res);
+ this.router.handle(this.req, this.res);
});
it('should call /home.read() as default', function(done) {
- req.path = '/';
- req.method = 'GET';
+ this.req.path = '/';
+ this.req.method = 'GET';
var spy = sinon.spy(controllers['/home'], 'read');
- var res = {
+ this.res = {
send: function() {
- assert.ok(spy.called);
+ spy.called.should.be.ok;
+
spy.restore();
done();
},
status: function() {}
}
- var router = new Router(app, controllers);
- router.handle(req, res);
+ this.router.handle(this.req, this.res);
});
it('should call next(code:404) if controller does not exist', function(done) {
- req.path = '/fsdf/hgd';
- var res = {}
+ this.req.path = '/fsdf/hgd';
var next = function(err) {
- assert.strictEqual(err.code, 404);
+ err['code'].should.equal(404);
+
done();
}
- var router = new Router(app, controllers);
- router.handle(req, res, next);
+ this.router.handle(this.req, this.res, next);
})
it('should call /home.foo() with 2 arguments', function(done) {
- req.path = '/home/foo/x/y';
+ this.req.path = '/home/foo/x/y';
var spy = sinon.spy(controllers['/home'], 'foo');
- var res = {
+ this.res = {
send: function() {
- assert.ok(spy.called);
+ spy.called.should.be.ok;
+
spy.restore();
done();
},
status: function() {}
}
- var router = new Router(app, controllers);
- router.handle(req, res);
+ this.router.handle(this.req, this.res);
});
it('should call /home.read() with 2 arguments', function(done) {
- req.path = '/home/x/y';
- req.method = 'GET';
+ this.req.path = '/home/x/y';
+ this.req.method = 'GET';
var spy = sinon.spy(controllers['/home'], 'read');
- var res = {
+ this.res = {
send: function() {
- assert.ok(spy.called);
+ spy.called.should.be.ok;
+
spy.restore();
done();
},
status: function() {}
}
- var router = new Router(app, controllers);
- router.handle(req, res);
+ this.router.handle(this.req, this.res);
});
it('should call /home.camelCase()', function(done) {
- req.path = '/home/camel-case';
+ this.req.path = '/home/camel-case';
var spy = sinon.spy(controllers['/home'], 'camelCase');
- var res = {
+ this.res = {
send: function() {
- assert.ok(spy.called);
+ spy.called.should.be.ok;
+
spy.restore();
done();
},
status: function() {}
}
- var router = new Router(app, controllers);
- router.handle(req, res);
+ this.router.handle(this.req, this.res);
});
it('should call /home/baz(bar) instead of /home.baz.bar()', function(done) {
- req.path = '/home/baz/bar';
+ this.req.path = '/home/baz/bar';
var failSpy = sinon.spy(controllers['/home/baz'], 'bar');
var spy = sinon.spy(controllers['/home'], 'baz');
- var res = {
+ this.res = {
send: function() {
- assert.ok(spy.called);
- assert.equal(failSpy.called, false);
+ spy.called.should.be.ok;
+ failSpy.called.should.not.be.ok;
+
spy.restore();
failSpy.restore();
done();
},
status: function() {}
}
- var router = new Router(app, controllers);
- router.handle(req, res);
+ this.router.handle(this.req, this.res);
});
it('should map POST to create (no arguments)', function(done) {
- req.path = '/crud';
- req.method = 'POST';
+ this.req.path = '/crud';
+ this.req.method = 'POST';
var spy = sinon.spy(controllers['/crud'], 'create');
- var res = {
+ this.res = {
send: function() {
- assert.ok(spy.called);
+ spy.called.should.be.ok;
+
spy.restore();
done();
},
status: function() {}
}
- var router = new Router(app, controllers);
- router.handle(req, res);
+ this.router.handle(this.req, this.res);
});
it('should map GET to read (with argument)', function(done) {
- req.path = '/crud/1';
- req.method = 'GET';
+ this.req.path = '/crud/1';
+ this.req.method = 'GET';
var spy = sinon.spy(controllers['/crud'], 'read');
- var res = {
+ this.res = {
send: function() {
- assert.ok(spy.calledWithExactly('1'));
+ should.ok(spy.calledWithExactly('1'));
+
spy.restore();
done();
},
status: function() {}
}
- var router = new Router(app, controllers);
- router.handle(req, res);
+ this.router.handle(this.req, this.res);
});
it('should map PUT to update (with argument)', function(done) {
- req.path = '/crud/1';
- req.method = 'PUT';
+ this.req.path = '/crud/1';
+ this.req.method = 'PUT';
var spy = sinon.spy(controllers['/crud'], 'update');
- var res = {
+ this.res = {
send: function() {
- assert.ok(spy.calledWithExactly('1'));
+ should.ok(spy.calledWithExactly('1'));
+
spy.restore();
done();
},
status: function() {}
}
- var router = new Router(app, controllers);
- router.handle(req, res);
+ this.router.handle(this.req, this.res);
});
it('should map DELETE to del (with argument)', function(done) {
- req.path = '/crud/1';
- req.method = 'DELETE';
+ this.req.path = '/crud/1';
+ this.req.method = 'DELETE';
var spy = sinon.spy(controllers['/crud'], 'del');
- var res = {
+ this.res = {
send: function() {
- assert.ok(spy.calledWithExactly('1'));
+ should.ok(spy.calledWithExactly('1'));
+
spy.restore();
done();
},
status: function() {}
}
- var router = new Router(app, controllers);
- router.handle(req, res);
+ this.router.handle(this.req, this.res);
});
it('should map GET edit to edit (with argument)', function(done) {
- req.path = '/crud/edit/1';
- req.method = 'GET';
+ this.req.path = '/crud/edit/1';
+ this.req.method = 'GET';
var spy = sinon.spy(controllers['/crud'], 'edit');
- var res = {
+ this.res = {
send: function() {
- assert.ok(spy.calledWithExactly('1'));
+ should.ok(spy.calledWithExactly('1'));
+
spy.restore();
done();
},
status: function() {}
}
- var router = new Router(app, controllers);
- router.handle(req, res);
+ this.router.handle(this.req, this.res);
});
it('should map GET add to add (without argument)', function(done) {
- req.path = '/crud/add';
- req.method = 'GET';
+ this.req.path = '/crud/add';
+ this.req.method = 'GET';
var spy = sinon.spy(controllers['/crud'], 'add');
- var res = {
+ this.res = {
send: function() {
- assert.ok(spy.called);
+ spy.called.should.be.ok;
+
spy.restore();
done();
},
status: function() {}
}
- var router = new Router(app, controllers);
- router.handle(req, res);
+ this.router.handle(this.req, this.res);
});
it('should not map POST to create is resource is false', function(done) {
- req.path = '/noncrud';
- req.method = 'POST';
+ this.req.path = '/noncrud';
+ this.req.method = 'POST';
var spy = sinon.spy(controllers['/noncrud'], 'create');
- var res = {
+ this.res = {
send: function() {
- assert(spy.called === false);
+ spy.called.should.not.be.ok;
+
spy.restore();
done();
},
status: function() {}
}
var next = function(err) {
- assert.strictEqual(err.code, 404);
+ err['code'].should.equal(404);
+
done();
}
- var router = new Router(app, controllers);
- router.handle(req, res, next);
+ this.router.handle(this.req, this.res, next);
});
-
-
});
});
View
2 test/testdir/controllers/dir.js
@@ -1 +1 @@
-exports.file = 'file';
+exports.bar = 'bar';
View
4 test/testdir/controllers/dir/file.js
@@ -1,3 +1,3 @@
-exports.method = 'method';
+exports.foo = 'foo';
-exports.method2 = 'method2';
+exports.bar = 'bar';
View
0 test/testdir/controllers/dir/file/method.js → ...tdir/controllers/dir/subdir/controller.js
File renamed without changes.
View
3 test/testdir/controllers/dir2/dir3/file3.js
@@ -1,3 +0,0 @@
-exports.nil = 'nil';
-
-exports.method4 = 'method4';
View
2 test/testdir/controllers/with_extension.controller.js
@@ -1 +1 @@
-exports.method = 'method';
+exports.baz = 'baz';

0 comments on commit 3d58dcb

Please sign in to comment.
Something went wrong with that request. Please try again.