Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I have inherited a node-js project, and I am completely new to it. Fortunately, it is already covered by unit tests.

The class under test, FmsRescuers, has the responsibility of sending HTTP requests to a remote server.

My first refactoring was to extract the HTTP behavior into the HttpRequest class, to allow testing that an operation requests the correct url with the correct data, without actually performing the http request. The HttpRequest class did not previously exist.

I am looking for feedback on the unit test from a perspective of best practices. In particular, how do I best avoid the duplication of calling the checkIn function through the different tests?

require('../server/extensions');
var FmsRescuers = require('../server/fms_rescuers');
var Rescuer = require('./stubs/helper').Rescuer;

var requestedPath;
var requestedStationNumber;
var requestedRescuerNumber;
var httpRequest = {
    post: function(path, requestData, callback) {
              requestedPath = path;
              requestedStationNumber = requestData.stationNumber;
              requestedRescuerNumber = requestData.rescuerNumber;
              callback('', 200);
          }
};
var rescuers = new FmsRescuers(httpRequest);    
var rescuer = new Rescuer(42, 'Name', new Date().addHours(-2).toTimeZonedString(), new Date().addHours(-1).toTimeZonedString());

exports.testCheckIn = {
    setUp: function(callback) {
               requestedPath = undefined;
               requestedStationNumber = undefined;
               requestedRescuerNumber = undefined;
               callback();
           },
    'check in requests correct path' : function(test) {
        rescuers.checkIn(rescuer, 123, 3, function(result, status) {
            test.equal(requestedPath, '/attendance/checkin');
            test.done();
        });
    },

    'check in sends correct station number' : function(test) {
        rescuers.checkIn(rescuer, 123, 3, function(result, status) {
            test.equal(requestedStationNumber, 123);
            test.done();
        });
    },

    'check in sends correct rescuer number' : function(test) {
        rescuers.checkIn(rescuer, 123, 3, function(result, status) {
            test.equal(requestedRescuerNumber, 42);
            test.done();
        });
    }
};
share|improve this question
add comment

1 Answer

You could use sinon.js to have the power of spies, stubs and mocks. http://sinonjs.org/

That way you could create a stub of httpRequest.

Look for stub.yield([arg1, arg2, ...]).

Edit I added a code example. I did not fully tested the code, but it's how it would look like.

var sinon = require('sinon');
require('../server/extensions');
var FmsRescuers = require('../server/fms_rescuers');
var Rescuer = require('./stubs/helper').Rescuer;

var mockedHttpRequest;
var rescuers;
var rescuer;

exports.testCheckIn = {    
    setUp: function(done) {
        var httpRequest { post: function () {} };
        mockedHttpRequest = sinon.mock(httpRequest);            
        rescuers = new FmsRescuers(mockedHttpRequest);   
        rescuer = new Rescuer(42, 'Name', new Date().addHours(-2).toTimeZonedString(), new Date().addHours(-1).toTimeZonedString());

    },

    'check in requests correct path' : function(test) {
        var expectedPath = '/attendance/checkin'
        mockedHttpRequest
                .expects('post')
                .once()
                .withArgs(expectedPath)
                .yields('', 200);

        rescuers.checkIn(rescuer, 123, 3, function(result, status) {
            mockedHttpRequest.verify();
            test.equal(result, '');
            test.equal(status, 200);
            test.done();
        });
    },
};
share|improve this answer
    
I added a code sample. –  jackdbernier Feb 6 at 12:49
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.