Tell me more ×
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

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

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

Browse other questions tagged or ask your own question.