1
date.js 測試在JS日期
class DateHelper {
constructor() {
this.months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
}
postedOn(from) {
const date = new Date(from);
const day = date.getDate();
const month = this.months[date.getMonth()];
const year = date.getFullYear();
if (year === new Date().getFullYear()) {
return `${day} ${month}`;
}
return `${day} ${month} ${year}`;
}
}
date_spec.js
describe('',() => {
it('',() => {
const from = 'Wed Feb 25 2015 00:38:24 GMT+0200 (EET)';
expect(newDateHelper.postedOn(from)).to.equal('25 Feb 2015');
});
it('',() => {
const from = 'Wed Feb 25 2017 00:38:24 GMT+0200 (EET)';
expect(newDateHelper.postedOn(from)).to.equal('25 Feb');
});
});
所有的測試都通過當前,但由於postedOn
比較當年我將需要更新這些測試每年。我怎樣才能解決這個問題?
沒想過。感謝您的幫助) –
@ArtemGurzhii如果沒關係,您可以將答案標記爲正確) – Antonio