Friday, July 9, 2010

Try Testing Fab Apps with Vows.js

‹prev | My Chain | next›

Today, I am back on testing with vows.js. I have one last fab.js library to check, unary_try. In the application, it is used like this:
  ( /move/ )
( unary_try( function () {
update_player_status(JSON.parse(""+this));
broadcast(comet_walk_player(this));
} ) )
When a client accesses the /move resource, the unary_try function creates a unary (upstream) application that invokes the supplied anonymous function if the downstream (browser) supplies a body / POST data. The unary_try then closes the connection with an empty response—the purpose is to update the local data store only.

First up, I test that the callback gets called:
    'with a body': {
topic: api.fab.callback_when_send_obj({body: "foo"}),

'invokes callback with body': function(obj) {
assert.equal(obj, "foo");
}
}
That passes, but I could really use a better API test call than "callback_when_send_obj". Ick. I will leave that for now because I want to see if other calls are needed.

Next up, I want to be able to test that the downstream app gets called with empty output. That requires another vows.js topic. This time, I am not interested in verifying that the supplied callback gets called, now I want to know what the downstream (e.g. the browser gets called with), so I add to my list of API calls:
    downstream_when_send_obj: function(obj) {
return function () {
var topic = this;
var upstream_listener = unary_try(
function() {}
).call(
function(obj) { topic.callback(null, obj); }
);

upstream_listener(obj);
};
}
From the top, the topic is from vows.js itself. I supply unary_try with an empty, anonymous function (again, I do not care about the callback in this case). I then call the function with a faux downstream app. Lastly, I need to send data to the upstream app which I do via the upstream_listener.

With that, I can test the response when sending data from browsers to unary_try:
    'downstream, with a body': {
topic: api.fab.downstream_when_send_obj({body: "foo"}),
'terminates connection': function(obj) {
assert.isUndefined(obj);
}
}
After a few more of those, I have my last independent (fab) app covered:
cstrom@whitefall:~/repos/my_fab_game$ vows --spec

♢ unary_try

[unary_try] error: undefined (undefined)
callback, with a body
✓ invokes callback with body
downstream, with a body
✓ terminates connection
downstream, with HTTP headers
✓ terminates connection
downstream, with empty request
✓ terminates connection
downstream, with exception
✓ terminates connection
...
✓ OK » 18 honored (0.106s)
Up tomorrow, I may clean-up / refactor these tests. Otherwise I will move onto testing some of the (fab) apps that store state.


Day #159

No comments:

Post a Comment