2016-08-31 6 views
0

私は、momentを使用するMochaで単体テストしようとしている機能があります。ここでMochaとMomentjsによるユニットテスト

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

は、私がこれまで持っているものですが、私はトラブルmomentをどうするかを考え出すを抱えています。 makeRange(40)に電話してテストを実行すると、文字列は毎回異なります。

あなたが知られている日付/時刻が使用されることを保証するために、コントローラに必須ですmomentモジュールを模擬することができますので、あなたは、rewireを使用しているどのように私は偽の現在の時刻(すなわち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() { 
    // 
    }); 
}); 
+0

現在の時刻を特定の形式で取得するだけでいいですか? –

答えて

0

?:

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

    var makeRange; 

    beforeEach(function() { 

    var momentMock = function() { 
     return moment('2016-08-31T09:00:00Z'); 
    }; 
    controller.__set__("moment", momentMock); 
    makeRange = controller.__get__("makeRange"); 
    }); 

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

    expect(makeRange(40)).to.equal('2016-08-31T08:20:00.000Z,2016-08-31T09:00:00.000Z'); 
    }); 
}); 
関連する問題