我有一个需要编写单元测试的功能:
public refresh(): void {
this.closeAllPopups().subscribe(() => {
const refreshButton = $('.refresh').last();
if (refreshButton.length) {
refreshButton[0].click();
}
});
}
我想知道如何设置$('。refresh')。last()。length不为0。
我的单元测试1如下:
it('should click refresh button element', fakeAsync(() => {
spyOnProperty($('.refresh').last(), 'length').and.returnValue(1);
console.log($('.refresh').last());
spyOn(actions, 'closeAllPopups').and.returnValue(Observable.of(true));
spyOn($.fn, 'click');
actions.refresh();
tick();
expect($.fn.click).toHaveBeenCalled();
}));
我的单元测试2如下:
it('should click refresh button element', fakeAsync(() => {
const button = document.createElement('div');
button.setAttribute('class', 'refresh');
console.log($('.refresh').last());
spyOn(actions, 'closeAllPopups').and.returnValue(Observable.of(true));
spyOn($.fn, 'click');
actions.refresh();
tick();
expect($.fn.click).toHaveBeenCalled();
}));
所有单元测试console.log($('.refresh').last())
都显示n.fn.init.length =0。如何更改它?