vendredi 24 avril 2015

How to mock the dt variable and s parameter in this Javascript function using Jasmine testing

I have following Javascript function against which I am trying to write a unit test.

Is there any way I can tell the unit test to use certain values for 'dt' and 's' varaiables.

       function setCurrentTimeShift() {
        var dt = new Date();
        var currentShift = _.find(shiftComparisonWidgetSettings.shifts, function (s) {
            var start =  getScaledDate(s.startTime, dt);
            var end = getScaledDate(s.endTime, dt);
            return (dt >= start && dt < end && start < end) ||
               (dt > start && dt > end && start > end) ||
               (dt < start && dt > end && start > end);
        });
        if (currentShift) {
            shiftComparisonWidgetSettings
                    .selectedShiftId =
                  currentShift.shiftId;
            shiftComparisonWidgetSettings
                    .selectedShiftName = 
                     currentShift.shiftName;
        }
    }

The unit test I have is as below.

       describe('setCurrentTimeShift function', function () {
        it('should return the value 1', function () {
            var vm = controller('ShiftComparisonWidgetSettingsController', { $scope: scope });
            vm.shifts = shifts;
            var dt = new Date(2015, 4, 20, 9, 0, 0);
            vm.setCurrentTimeShift();
            expect(vm.selectedShiftId).to.equal(1);
        });
    });
});

Aucun commentaire:

Enregistrer un commentaire