2017-02-25 1 views
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は現在の年と比較するので、私はこれらのテストを更新する必要があります毎年。これをどうすれば解決できますか?

答えて

2

テンプレートリテラルとその中の今年の成果についてどう思いますか?

const from = `Wed Feb 25 ${new Date().getFullYear()} 00:38:24 GMT+0200 (EET)`; 
+0

これについては考えていません。助けてくれてありがとう) –

+0

@ArtemGurzhiiあなたが正しい場合は正しい答えとしてマークすることができます) – Antonio