Skip to content

Commit

Permalink
Improved test helper
Browse files Browse the repository at this point in the history
  • Loading branch information
1602 committed Apr 17, 2012
1 parent 6db367c commit 26eba7d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 17 deletions.
2 changes: 0 additions & 2 deletions templates/config/routes.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
exports.routes = function (map) {



// Generic routes. Add all your routes below this line
// feel free to remove generic routes
map.all(':controller/:action');
Expand Down
37 changes: 22 additions & 15 deletions templates/test_helper.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
var app = require('railway').createServer();
app.enable('quiet');
app.enable('models cache');
app.enable('eval cache');


exports.controller = function (controllerName, exp) {

Expand All @@ -7,7 +11,6 @@ exports.controller = function (controllerName, exp) {
// trick
it('init', function (test) { process.nextTick(function () { test.done(); }); });
var nu = process.env.NODEUNIT_PATH || 'nodeunit';
console.log(nu);

var http = require('http');
var sinon = require('sinon');
Expand Down Expand Up @@ -81,10 +84,8 @@ exports.controller = function (controllerName, exp) {
var test = this;
var origDone = test.done;
test.done = function () {
console.log('call', n);
if (--n === 0) {
test.done = origDone;
console.log('hey');
test.done();
}
}.bind(this);
Expand All @@ -93,14 +94,12 @@ exports.controller = function (controllerName, exp) {
return test;
};

app.enable('quiet');
app.enable('models cache');

var cookies = {};
var csrfToken = '';

function stubRequest(method, _method) {
return function (url, callback) {

var req = new http.IncomingMessage;
var res = new http.ServerResponse({method: 'NOTHEAD'});

Expand All @@ -113,36 +112,36 @@ exports.controller = function (controllerName, exp) {
};

req.headers = {
host:'localhost'
host:'localhost',
cookie: cookieString(cookies)
};

req.flash = sinon.spy(req.flash);
req.connection = {};
req.url = url;
req.method = method;
req.cookies = cookies;

if (method === 'POST') {
if (typeof callback === 'object')
{
if (typeof callback === 'object') {
req.body = callback;
callback = arguments[2];
}

if (_method)
{
if (_method) {
req.body._method = _method;
}

req.body.authencity_token = csrfToken;
req.body.authenticity_token = csrfToken;

}

res.end = function () {
this.req = req;
this.res = res;
var cook = res.headers['set-cookie'].split(';').shift().split('=');
cookies[cook[0]] = cook[1];
if (res.headers['set-cookie']) {
var c = res.headers['set-cookie'].split(';').shift().split('=');
cookies[c[0]] = c[1];
}
csrfToken = req.csrfToken;
callback.call(this);
}.bind(this);
Expand All @@ -151,6 +150,14 @@ exports.controller = function (controllerName, exp) {
}
};

function cookieString(obj) {
var s = [];
Object.keys(obj).forEach(function (val) {
s.push(val + '=' + obj[val]);
});
return s.join('; ');
}

};

var group_name = false, EXT_EXP;
Expand Down

0 comments on commit 26eba7d

Please sign in to comment.