0
我正在使用摩卡來針對新寫入的類運行測試,並且需要構建一個Event
以進行比較。我計劃使用對象存根,並將它們替換爲Event
類的實際實例,這些實例由於數據庫連接使用而具有異步構造函數。所以我使用遞歸調用來按順序處理存根。 這裏是問題:所有我的存根(stub)對象都被最新的實例取代,我不知道爲什麼。請解釋我錯在哪裏。替換對象屬性時出現異常beahviour
Event.coffee:
class Event
start = 0
duration = 0
title = ""
atype = {}
constructor: (_start, _duration, _title, _atype, cb) ->
start = _start
duration = _duration
title = _title
evt = @
ActivityType.find({} =
where: {} =
title: _atype
).success((res) ->
atype = res
cb? evt
).error(() ->
throw new Error "unable to assign atype '#{_atype}'"
)
# ...
Event.test.coffee:
# ...
suite "getEventAt",() ->
events =
FREE: {} =
start: 0
duration: Day.MINUTES_PER_DAY
title: "Free time"
type: "FREE"
REST: {} =
start: 10
duration: 30
title: "rest"
type: "_REST"
FITNESS: {} =
start: 30
duration: 30
title: "fitness"
type: "_FITNESS"
WORK: {} =
start: 20
duration: 30
title: "work"
type: "_WORK"
suiteSetup (done) ->
buildEvent = (ki) ->
ks = Object.keys events
((k) ->
v = events[k]
new Event v.start, v.duration, v.title, v.type, (e) ->
events[k] = e
if k == ks[ks.length-1]
return done?()
return buildEvent(ki+1)
)(ks[ki])
buildEvent(0)
# ...