我有一个我已更新的功能,使用Moment JS来比较两个日期。该函数工作正常,但测试失败表明undefined is not a function (evaluating Date.UTC)
。这是测试的愚蠢版本(Jasmine):
var dateChecker,
$rootScope;
var AppParams = {
dateToCheck: '9/1/2015'
};
beforeEach(inject(function (_$rootScope_) {
$rootScope = _$rootScope_;
$rootScope.AppParams = AppParams;
}));
var createDate = function (year, month, date) {
var overrideDate = new Date(year, month, date);
spyOn(window, 'Date').andCallFake(function () {
return overrideDate;
});
};
describe('check date', function () {
beforeEach(function () {
createDate(2015, 12, 10);
inject(function (_dateChecker_) {
dateChecker = _dateChecker_;
});
});
it('should return 1/1/2016', function () {
var dates = dateChecker.checkDate();
expect(dates.length).toBe(1);
expect(_.contains(dates, '01/01/2016')).toBe(true);
});
});
这是它正在测试的功能的一个愚蠢的版本:
this.checkDate = function () {
var dates;
var dateToCheck = $rootScope.AppParams.dateToCheck;
var today = moment();
if (today.isAfter(moment(dateToCheck, 'MM/DD/YYYY'), 'day')) {
dates = [
'01/01/2016'
];
}
return dates;
};
在逐个分开功能之后,我已经确定在评估moment(dateToCheck, 'MM/DD/YYYY')
时发生了错误。我已经确认today
和dateToCheck
都已正确分配。
我错过了什么?
修改