spyon
函数抛出错误,提示Expected spy scrollIntoView to have been called
下面是我的component的代码片段:
@Component({
template: `
<form appFocusFirstInvalidField>
<input type="text" class="ng-invalid" onFocus>
<button type="submit"></button>
</form>
`
})
class FocusFirstInvalidInputComponent {
constructor(private el: ElementRef) {
console.log('in constructor');
}
}
下面是测试用例,这里我为输入元素添加了spyon函数,但是抛出了错误。
describe('Directive: onFocus', () => {
let fixture: ComponentFixture<FocusFirstInvalidInputComponent>;
let inputEl: HTMLElement;
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [FocusFirstInvalidInputComponent, FocusFirstInvalidFieldDirective]
});
fixture = TestBed.createComponent(FocusFirstInvalidInputComponent);
inputEl = fixture.nativeElement.querySelector('input');
});
it('restrict should paste', () => {
fixture.detectChanges();
const event = new Event('submit', { bubbles: true });
spyOn(inputEl, 'scrollIntoView');
inputEl.dispatchEvent(event);
expect(inputEl.scrollIntoView).toHaveBeenCalled();
});
}); ```