2017-10-16 49 views
2

我想創建一個測試,我不能讓它工作,因爲當我在測試環境中做moment("2017-09-10")它不會創建一個Moment對象。正因爲如此,isBetween總是錯誤的。測試時間與酶

這是我的功能

export const filteredStatistics = (data, startDate, endDate) => { 
    if(startDate === null || endDate === null) return data.toJS() 

    return data.filter(element => { 
    const date = moment(element.get("date")) 

    return date.isBetween(startDate, endDate, "days", "[]") 
    }).toJS() 
} 

這是我的測試

import moment from 'moment' 

describe('all dates',() => { 
    it('returns original statistics',() => { 
     const statistics = getOverviewStatistics('ctr')(overview) 
     const statDate = moment(statistics.first().get('date')) 
     const endDate = moment(statistics.last().get('date')) 

     const current = filteredStatistics(statistics, statDate, endDate) 
     const expected = statistics.toJS() 

     expect(current).toEqual(expected) 
     }) 
    }) 

我使用的是 「時刻」: 「^ 2.19.1」, 「酶」:「^ 2.9.1 「,」反應「:」^ 15.4.1「。 你知道我該如何測試它?

打印與所述對象之間的差產生運行代碼和運行測試

的的console.log:

console.log('date:', date, 'startDate:', startDate, 'endDate', endDate, 'betweenResult:', date.isBetween(startDate, endDate, 'days', '[]')) 

運行代碼:
enter image description here

運行試驗:

enter image description here

PS:我更新爲使用給定的建議

我發現錯誤的代碼:我把結束日期2016和2017年不u.u」對不起,並感謝大家的幫助!

+0

它會拋出一個錯誤?你如何定義測試時機? – jmargolisvt

+0

@jmargolisvt它不會引發錯誤。它只是不通過,因爲返回一個empy數組。我定義這樣的時刻:從'時刻'進口時刻 – Marina

回答

1
  1. 更新到時刻2.19.1。有2.19.0的問題可能會影響您的導入。

  2. 請勿使用moment(new Date(string))。這繞開了Moment的解析功能,並使用對象的解析來代替 - 這是低劣的並且經常不一致。只需使用moment(string),或者使用moment(string, format)

+0

你好,謝謝你的迴應,但它沒有工作:(我更新了時刻到2.19.1,並刪除了新的日期(字符串),並得到了相同結果:/我發現這篇文章解釋了這個問題,但我不想嘲笑我將使用的每一個日期。文章:http://dancork.co.uk/2015/12/07/ stubbing-moment/ – Marina

+0

A添加一些圖像以顯示運行代碼的結果並運行測試以幫助解釋問題 – Marina

+0

您的圖像來自哪裏有點混淆,但對於我可以看到的值,「false」會是「date」不在'startDate'和'endDate'之間 –