這裏測試嵌套的對象是我的測試茉莉花
describe("setTimer", function() {
it("set status timer values from parameters and sets timer.visible to true", function(){
var boxNumber = 1,
time = 15;
myObject.setTimer(boxNumber, time);
expect(anotherObject.status.timer.boxNum).toBe(boxNumber);
expect(anotherObject.status.timer.seconds).toBe(time);
})
});
下面是代碼
setTimer: function (boxNum, seconds) {
anotherObject.status.timer.boxNum = boxNum;
anotherObject.status.timer.seconds = seconds;
anotherObject.status.timer.visible = true;
},
以下是錯誤我得到
TypeError: Cannot read property 'timer' of undefined
我嘗試設置使用對象anotherObject = {}
我試過設置anotherObject.status = {}
,最後試着設置anotherObject.status.timer = {}
,howeve r我仍然得到錯誤。任何想法,我怎樣才能嘲笑對象?
目前還不清楚「anotherObject」是從哪裏提供的。這是否在您的主要「describe」函數中?別的地方?上面的代碼片段似乎沒有提供足夠的上下文來回答問題。 –
@AndrewTheken,anotherObject實際上是一個全局對象,它是由當前測試範圍之外的另一種方法創建的。這就是爲什麼我認爲我可能不得不嘲笑測試範圍內的整個對象。 – zmanc
這是在節點或瀏覽器,或其他地方? –