Skip to Content

Behaviour-driven node.js development with stubbing by combining Vows and node-gently

Posted on    One min read

I’m about 3.5 hours into node.js development, I guess that qualifies me to give advice on it on this Internet thing.

Being the BDD fanatic that I am, I wanted to start off behaviour-driven right from the beginning, and Vows looked like a good choice.

However, I quickly came to the point where I needed to stub out a dependency in one of my modules, and as far as I can see, Vows doesn’t provide mocking/stubbing. But https://github.com/felixge/node-gently does, and here is my approach at combining these two:

This is the Vows spec:

var gently = global.GENTLY = new (require("gently"));

var vows = require("vows"),
    assert = require("assert");

var myModule = require("MyModule");

vows.describe("My Module").addBatch({
  "when calling its foo() method": {
    topic: myModule,
    "it triggers a console message": function (topic) {
      gently.expect(gently.hijacked.sys, "puts", function(str) {
        assert.equal(str, "Hello World");
      });
      topic.foo("Hello World");
    }
  }
}).export(module);

And this is the implementation of MyModule:

if (global.GENTLY) require = GENTLY.hijack(require);

var sys = require("sys");

function foo(message) {
  sys.puts(message);
  return true;
}

exports.foo = foo;

No idea if this makes any sense in the long run – I will tell you when I’m about 14 hours or so into BDD node.js development…

comments powered by Disqus