3
我試圖測試一個我創建的,做一些與滾動的東西的部件。我正在使用Jasmine和jasmine-jquery進行測試,但我收到了一些奇怪的結果。茉莉花 - 茉莉花jquery spyOnEvent
我想測試一下,當頁面以特定方式滾動時,我的小部件成功觸發事件。我在這裏簡化了一些事情,以防萬一它是我的小部件導致問題的原因,所以我只是想測試窗口上是否觸發了滾動事件。
it("should trigger scrollEvent on the window", function(){
loadFixtures('lorem-ipsum.html'); //gives some height to the page
window.scrollTo(0,0);
console.log($(window).scrollTop()); //Should return 0
$(window).bind("scroll", function (e) {
console.log("Now scrolling");
});
window.scroll(0,100);
console.log($(window).scrollTop()); //Should return 100
window.scroll(0,50);
console.log($(window).scrollTop()); //Should return 50
expect('scroll').toHaveBeenTriggeredOn(window);
});
在控制檯中我得到以下
Testing started at 16:32 ...
0
100
50
Expected event scroll to have been triggered on [object Object]
()@C:/Projects/jquery.scrollevents/tests/specs/development.js:45
'Now scrolling'
Expected event scroll to have been triggered on [object Object]
Error: Expected event scroll to have been triggered on [object Object]
at null.<anonymous> (C:/Projects/jquery.scrollevents/tests/specs/development.js:45:34)
0
100
50'Now scrolling'
Process finished with exit code 0
我不知道怎麼利用這個作爲事件的順序是奇數。它運行兩次?滾動事件只在預期後觸發?
我已經在瀏覽器中調試過它,你可以看到同樣的事情發生。首先測試失敗,然後觸發瀏覽器事件。
我看了茉莉花的異步功能,但我不知道我在等什麼?
任何幫助將不勝感激。謝謝