links for 2010-12-28

December 29, 2010


links for 2010-12-23

December 24, 2010

links for 2010-12-22

December 23, 2010

links for 2010-12-21

December 22, 2010

Dependency injection is a handy technique for writing testable code. If you’ve used Spring in the Java world, you’re probably familiar with it. Put simply, it means that code should not be responsible for instantiating the things it depends on – these dependencies should be passed in by the calling code. That means you can pass in mock versions for testing.

In node.js land, we use require to bring in external modules. For example:

var http = require('http');
exports.twitterData = function(callback) {
  http.createClient(... blah...);

This module is hard to test without needing internet access, which may not be available if your tests are running on a build server behind a corporate firewall. Besides, you don’t want your tests calling twitter all the time.

What you want to be able to do is replace http with a mock version, so that you can verify that your module makes all the right calls without actually needing access to the internet. There are a couple of ways to do this, like messing about with require.paths to bring in mock modules. Here’s the way we decided to do it.

First we write our modules in this style:

module.exports = function(http) {
  var http = http | require('http');
  // private functions and variables go here...

  //return the public functions
  return {
    twitterData: function(callback) {

In normal use, we’d require our twitter module like this:

var twitter = require('twitter')();

In our tests, we’d require it like this:

var mockHttp = { createClient: function() { assert(something); } };
var twitter = require('twitter')(mockHttp);
//do some tests.

It seems to be working fine for us so far, and having that first line of your module tell you explicitly what the dependencies are is quite handy as well. If you want some real-world examples of this, take a look at the log4js-node source.

links for 2010-12-16

December 17, 2010

We’re Hiring!

December 16, 2010

Sensis is looking for two Senior Developers to join our happy band of code monkeys here at the Verticals team. We look after Citysearch® mainly, but we have our fingers in several pies. Don’t worry, we washed our hands.

If you’re clever, like playing with new technologies, and want a chance to work with a team who are passionate about TDD and automation, we’d like to hear from you. If you want a chance to make cool stuff with CouchDB, node.js, Cucumber testing, and get paid for it – we’d like to talk to you.

If you’re interested, email Sensis Careers, quoting reference “3B/54388” with your CV/resume/link to your github projects, or just more questions.