mercredi 31 août 2016

Unit testing with Mocha and Momentjs

I have a function that I'm trying to unit test with Mocha that uses moment.

function makeRange(timeagoMinutes) {
  var endDate = moment().toISOString();
  var startDate = moment().subtract(timeagoMinutes, 'm').toISOString();
  return startDate + ',' + endDate;
}

Here is what I have so far, but I'm having trouble figuring out what to do with moment. If I call makeRange(40) and run the tests, the string is different each time.

How can I fake the current time (i.e. moment().toISOString()?

var rewire = require('rewire');
var controller = rewire('../thecontroller.js');
var moment = require('moment');

describe.only('makeRange', function() {
  var makeRange;

  beforeEach(function() {
    makeRange = controller.__get__('makeRange');
  });

  it('should return a string with a start date and end date', function() {
    //
  });
});

Aucun commentaire:

Enregistrer un commentaire