Testing REST API with LearnBoost’s Tobi + Vows.js

I've been looking for a clean, framework-independent way of doing white-box API testing using Node.js. For a long while, the things that popped up when doing a quick scan of the Node Package Manager package lists weren't ticking all of the right boxes: zombie.js is going for a full browser simulation but doesn't provide a simple Browser.post() method (you have to use selectors to find the form.submit button and then fire a click() on it), Node's native http.Client is too low-level and doesn't do cookies, and various other http request wrappers weren't quite cutting it either. I think I've found a solution for this particular version of the problem: LearnBoost's Tobi combined with Vows.js is letting me do clean REST API testing, with a minimum of hassle, and all the built-in sugar-coated goodness of should.js fluent assertions. For example: Future steps then include using the macros to help set up other tests that require a valid user, etc. Overall, this is the most straightforward solution I've yet found for the problem of testing a REST API while also faking a session.

6 thoughts on “Testing REST API with LearnBoost’s Tobi + Vows.js”

  1. Thanks for this. One thing your post does not mention is that it doesn’t seem possible to access $ from the callback. It’s always undefined in my testing and afaik it’s an incompatibility between vows and tobi.

    1. That is odd. I didn’t have much of a use for Sizzle selectors, though, since the tests were operating 100% on JSON object responses.

  2. Take Care !!! One need to set the headers (esp. ‘content-type’) in the options before using browser.post(…).

  3. In the code (line#46) below the data is a misnomer !! it should be options.

    browser.post(‘/api/user/create’, data, this.callback);

  4. I too had problems accessing $ in the callback (vows 0.5.8). Vows likes callbacks to be in the style of callback(err, res, $) clearing other parameters if err is set. I’m now using a wrapper function to add a null err parameter:

    browserGet = function(that, url) {
    var browser = tobi.createBrowser(3000, ‘localhost’);
    browser.get(url, function (res, $) {
    that.callback(null, res, $);
    });
    return browser;
    };

    and call browserGet(this, ‘/foo’) in the vows topic function.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.