不明白爲什麼這段代碼不起作用?
錯誤是:
Expected spy addEventListener to have been called with [ 'scroll', <jasmine.any(Function)>, true ] but it was never called.
。
describe('sessionActivity.spec',() => {
beforeEach(module('sessionActivity'));
it('should subscribe', inject(($window, sessionActivity) => {
spyOn($window, 'addEventListener');
sessionActivity.subscribe();
//$window.addEventListener('scroll',() => {}, true);
//$window.addEventListener('click',() => {}, true);
expect($window.addEventListener).toHaveBeenCalledWith('scroll', jasmine.any(Function), true);
expect($window.addEventListener).toHaveBeenCalledWith('click', jasmine.any(Function), true);
}));
});
在sessionActivity.subscribe裏面我創建了兩個事件監聽器。
而且如果我用//$window.addEventListener取消註釋兩行,則所有測試都是通過。
我應該在這裏改變什麼?
編輯:
的問題是,我的快捷裏面sessionActivity.subscribe():
const { addEventListener, removeEventListener } = $window;
addEventListener('scroll', trackActivityWithDebounce, true);
addEventListener('click', trackActivityWithDebounce, true);
有人能解釋一下嗎?
請提供所有相關的代碼。 addEventListener被調用的地方在哪裏?請參閱http://stackoverflow.com/help/mcve – estus