Friday, July 2, 2010

Testing Fab.js with Vows.js

‹prev | My Chain | next›

Last night, I got vows.js setup and running a dummy test. Tonight I would like to run it against the player_from_querystring fab.js app in my (fab) game.

In my "just_playing" suite, I require the player_from_querystring app:
var vows = require('vows'),
assert = require('assert');

var player_from_querystring = require('player_from_querystring').app;

var suite = vows.describe('just_playing');
That does not work, though:
cstrom@whitefall:~/repos/my_fab_game$ vows
module:238
throw new Error("Cannot find module '" + request + "'");
^
Error: Cannot find module 'player_from_querystring'
at loadModule (module:238:15)
at require (module:364:12)
at Object.<anonymous> (/home/cstrom/repos/my_fab_game/test/just_playing.js:4:31)
at Module._compile (module:384:23)
at Module._loadScriptSync (module:393:16)
at Module.loadSync (module:296:10)
at loadModule (module:241:16)
at require (module:364:12)
at /home/cstrom/bin/vows:326:19
at Array.reduce (native)
Changing the require statement to require('lib/player_from_querystring') (the fab apps are in the lib directory) does not help either. Now I get:
cstrom@whitefall:~/repos/my_fab_game$ vows 
module:238
throw new Error("Cannot find module '" + request + "'");
^
Error: Cannot find module 'lib/player_from_querystring'
...
So you cannot require with relative paths in node.js / commonjs. You can muck with the require paths:
var vows = require('vows'),
assert = require('assert');

require.paths.push("lib");
var player_from_querystring = require('player_from_querystring').app;

var suite = vows.describe('just_playing');
OK, so now I need to see if I can actually test that (fab) app that I required. I am keeping this as simple as possible for now. I add one batch of tests, with one topic and one actual test. I will worry about the terminology another day.

The actual test is from my existing test suite. It acts as a downstream (closer to the browser) fab app. It sends in a player query string and hopes to get a player object back:
var suite = vows.describe('just_playing').addBatch({
'Body': {
topic: function() {
var head = {
url: { search : "?player=foo&x=1&y=2" },
headers: { cookie: null }
};
var player;
function downstream (obj) { if (obj) player = obj.body; }

var upstream_listener = player_from_querystring.call(downstream);
upstream_listener(head);

return player;
},

'is player': function (player) {
assert.equal (player.id, "foo");
}
}}).export(module);
The test ('is player') asserts that the ID of the player object is "foo", which was set in the query string.

Amazingly, this just works:
cstrom@whitefall:~/repos/my_fab_game$ vows --spec

♢ just_playing

Body
✓ is player

✓ OK » 1 honored (0.083s)
I worked out most of the hard stuff when I first tested this app, but still, it is nice to know that I can get it converted so quickly to vows.js. It is even better to know that I can test my fab.js app with vows.js.


Day #152

No comments:

Post a Comment