我有這個測驗,完美運行:
it('The click on the logo must call goTo("home")', () => {
spyOn<LayoutComponent, any>(component, 'goTo');
let logo = fixture.debugElement.query(
By.css('#logoVitisoft')
).nativeElement;
logo.click();
fixture.whenStable().then(() => {
expect(component.goTo).toHaveBeenCalledWith('home');
});
});
而這個(與前一個幾乎不一樣)觸發了錯誤:
it('The click on Dashboard must call goTo(home)', () => {
spyOn<LayoutComponent, any>(component, 'goTo');
let button = fixture.debugElement.query(
By.css('#dashboardElem')
).nativeElement;
button.click();
fixture.whenStable().then(() => {
expect(component.goTo).toHaveBeenCalledWith('home'); /* ERROR SPAWN HERE */
});
});
精度:如果兩個測驗都通過“適合”呼叫,我已經禁用了測驗的隨機性,并且我繼續使用相同的種子執行 ng 測驗。當我將第二個測驗稱為“它”時,錯誤會產生:“預期是間諜,但得到了函式。”
編輯:這里是 beforeEach
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [RouterTestingModule, HttpClientModule],
declarations: [LayoutComponent],
}).compileComponents();
});
beforeEach(() => {
fixture = TestBed.createComponent(LayoutComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
我錯過了什么?
uj5u.com熱心網友回復:
fixture.whenStable()回傳一個異步執行的 Promise。這意味著您的測驗已經完成并在expect()執行時清理。
嘗試使用該done()函式進行這些型別的測驗:
it('The click on Dashboard must call goTo(home)', (done) => {
spyOn<LayoutComponent, any>(component, 'goTo');
let button = fixture.debugElement.query(
By.css('#dashboardElem')
).nativeElement;
button.click();
fixture.whenStable().then(() => {
expect(component.goTo).toHaveBeenCalledWith('home');
done();
});
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/397956.html
上一篇:測驗函式回傳選項<T>
