Angular 虽然存在expect,但规范没有expect控制台错误
我有需要期待的规范,但它说没有期望Angular 虽然存在expect,但规范没有expect控制台错误,angular,unit-testing,jasmine,karma-runner,Angular,Unit Testing,Jasmine,Karma Runner,我有需要期待的规范,但它说没有期望 it('should click on yes button of technician and check save&continue functionality', () => { const saveAndContinue = fixture.debugElement.query(By.css(saveContinueBtn)).nativeElement; saveAndContinue.click(); fixt
it('should click on yes button of technician and check save&continue functionality', () => {
const saveAndContinue = fixture.debugElement.query(By.css(saveContinueBtn)).nativeElement;
saveAndContinue.click();
fixture.detectChanges();
fixture.whenStable().then(() => {
const spy = spyOn(component,'isSaveAndContinueClicked').and.callThrough();
expect(component).toBeDefined();
expect(spy);
component.isSaveAndContinueClicked();
expect(component.isSaveAndContinueClicked).toHaveBeenCalled();
const yesbutton = fixture.debugElement.query(By.css('#yesButton')).nativeElement;
expect(yesbutton).toBeTruthy();
fixture.detectChanges();
fixture.whenStable().then(() => {
spyOn(component, 'change').and.callThrough();
spyOn(component, 'change2').and.callThrough();
yesbutton.click();
expect(component.change).toHaveBeenCalled();
expect(component.change2).toHaveBeenCalled();
});
});
});
它抛出错误,因为spec testComponent应单击技术人员的“是”按钮,并检查“保存并继续”功能是否没有预期。。。
您可以建议…您应该在
异步
或fakeAsync
块中添加回调,否则您的所有代码都将同步运行,而不会遇到任何期望的
这是因为您在fixture.whenStable()中有断言。然后(()=>{….})异步运行
it('should click on yes button of technician and check save&continue
functionality', async(() => {
const saveAndContinue =
fixture.debugElement.query(By.css(saveContinueBtn)).nativeElement;
saveAndContinue.click();
........
}));
关于这个问题,我没有使用async或waitForAsync构造。只有假货救了我的命。