Permalink
Browse files

experimenting with ways to test express

  • Loading branch information...
1 parent 1015690 commit 53aedb760bab04dff4f94f4396e2988f50066233 @dominictarr committed Jun 7, 2011
Showing with 156 additions and 0 deletions.
  1. +41 −0 handler.js
  2. +115 −0 test/handler.asynct.js
View
41 handler.js
@@ -0,0 +1,41 @@
+var it = require('it-is')
+
+module.exports =
+function Handler (){
+
+ function handleError(err,res){
+ var status =
+ 'number' == typeof err ? err : (err.statusCode || 500)
+ if(!handler.error[status])
+ status = 500
+ if('string' == typeof handler.error[status])
+ return res.render(handler.error[status], err)
+ handler.error[status](err,res)
+ }
+
+ function handler (controller, view){
+
+ it(controller).function ()
+ //it(view).typeof('string')
+
+ return function (req,res){
+ var render = function (err,obj){
+ if(err) return handleError(err,res)
+
+ if('string' == typeof view) {
+ res.render(view, obj, function (err,data){
+ if(err) return handleError(err)
+ res.send(data)
+ })
+ } else {
+ view(obj,res)
+ }
+ }
+ controller(req,render)
+ }
+ }
+ handler.error = {
+
+ }
+ return handler
+}
View
115 test/handler.asynct.js
@@ -0,0 +1,115 @@
+var it = require('it-is')
+ , Handler = require('../handler')
+
+exports ['pass call controller and then render'] = function (test){
+ var handler = Handler()
+
+ var action =
+ handler(function (req,render){
+ console.log('called controller')
+ render(null,{hello: 'asdf'})
+ }, 'example')
+
+ it(action).function()
+
+ var mockRes = {
+ render: function (view, obj, cb) {
+ console.log('called render')
+ it(view).equal('example')
+ it(obj).deepEqual({hello: 'asdf'})
+ cb(null,'<h1> hello asdf </h1>')
+ },
+ send: function (content){
+ console.log('called send')
+ it(content).equal('<h1> hello asdf </h1>')
+ test.done()
+ }
+ }
+
+ action({},mockRes)
+}
+
+exports ['view can be function'] = function (test) {
+ console.log('view can be function')
+ var handler = Handler()
+
+ var action =
+ handler(function (req, render) {
+ console.log('called controller')
+ render(null,{ hello: 'CRAZY' })
+ }, function (obj, res) {
+ res.send(JSON.stringify(obj))
+ })
+
+ it(action).function()
+
+ var mockRes = {
+ send: function (content) {
+ console.log('called send')
+ it(content).equal(JSON.stringify({ hello: 'CRAZY' }))
+ test.done()
+ }
+ }
+
+ action({}, mockRes)
+
+}
+
+exports ['can set error handler'] = function (test) {
+ console.log('can set error handler')
+ var handler = Handler()
+
+ handler.error[500] = function (err,res){
+ res.send(JSON.stringify(err))
+ }
+
+ var action =
+ handler(function (req, render) {
+ console.log('called controller')
+ render({ error: 'NO GOOD' })
+ }, 'wont-get-called')
+
+ it(action).function()
+
+ var mockRes = {
+ send: function (content) {
+ console.log('called send')
+ it(content).equal(JSON.stringify({ error: 'NO GOOD' }))
+ test.done()
+ }
+ }
+
+ action({}, mockRes)
+
+}
+
+//*/
+
+exports ['set error handler to a view name'] = function (test) {
+ console.log('can set error handler')
+ var handler = Handler()
+
+ handler.error[500] = '500'
+ var action =
+ handler(function (req, ready) {
+ console.log('called controller')
+ ready({ error: 'NO GOOD' })
+ }, 'wont-get-called')
+
+ it(action).function()
+
+ var mockRes = {
+ render: function (view, obj, cb) {
+ console.log('called render', view, obj)
+
+ it(view).equal('500')
+ it(obj).deepEqual({ error: 'NO GOOD' })
+ it(cb).equal(null)
+ test.done()
+ }
+ }
+
+ action({}, mockRes)
+
+}
+

0 comments on commit 53aedb7

Please sign in to comment.