Coffeescript 如何模拟日期对象?
我正试图为这种方法编写jasmine测试Coffeescript 如何模拟日期对象?,coffeescript,jasmine,Coffeescript,Jasmine,我正试图为这种方法编写jasmine测试 currentTime: -> Math.round(new Date().getTime()/1000) 在测试中,请尝试以下操作: @date = new Date() @date = Date(@date.getTime() + 70) spyOn(window, "Date").andCallFake -> @date expect(@user.currentTime()).toEqual xxx 我尝试了@date=ne
currentTime: ->
Math.round(new Date().getTime()/1000)
在测试中,请尝试以下操作:
@date = new Date()
@date = Date(@date.getTime() + 70)
spyOn(window, "Date").andCallFake ->
@date
expect(@user.currentTime()).toEqual xxx
我尝试了@date=newdate(@date.getTime()+70)
,但没有帮助
但我有一个错误:
Uncaught TypeError: Object [object Object] has no method 'getTime'
知道怎么修吗?谢谢解决方案来自
您必须模拟日期构造函数,以便它为
getTime
返回一个模拟,该模拟始终返回相同的int:
spyOn(window, "Date").andReturn {getTime: -> 1000}
Date(@Date.getTime()+70)
返回一个字符串。尝试新日期
。(虽然如果这是你的全部问题,那应该是[object String]
..)还有,为什么你要编写这个测试,它似乎需要比它要测试的内容更多的测试呢?@minitech,我已经试过了,但也有同样的错误
spyOn(window, "Date").andReturn {getTime: -> 1000}