我運行此測驗,但出現此錯誤:
Unhandled Promise rejection: <toHaveBeenCalledWith> : Expected a spy, but got Function.
it('should change myComponent value', () => {
spyOn(component.mychange, 'emit');
const input = fixture.debugElement.query(By.css('#mychange')).nativeElement;
input.value = "2";
fixture.whenStable().then(() => {
expect(component.mychange.emit).toHaveBeenCalledWith(2);
});
});
有類似的問題,但它發生在我身上的發射事件中,我嘗試了這些問題的解決方案,但它們不起作用。
uj5u.com熱心網友回復:
看到你在這里設定了一個 spy on 發射方法 - spyOn(component.mychange, 'emit');
問題似乎與觸發變化有關 -
所以你需要呼叫 component.mychange(10);
所以你的代碼會變成
it('should change myComponent value', async () => {
spyOn(component.mychange, 'emit');
const input = fixture.debugElement.query(By.css('#mychange')).nativeElement;
input.value = "2";
component.mychange(2);
await fixture.whenStable()
expect(component.mychange.emit).toHaveBeenCalledWith(2);
});
還請我使用 async await 這使它變得更好!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/357372.html
