【问题标题】:Spec has no expectation console error although expect is present尽管存在期望,但规范没有期望控制台错误
【发布时间】:2019-02-12 04:06:23
【问题描述】:

我有必须期待的规范,但它说没有期望......

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();
        });
   });
});

它会抛出错误,因为规范 testComponent 应该单击技术人员的“是”按钮并检查保存和继续功能没有预期...... 能不能给个建议...

【问题讨论】:

    标签: angular unit-testing jasmine karma-runner


    【解决方案1】:

    您应该在asyncfakeAsync 块中添加回调,否则您的所有代码将同步运行而不会遇到任何expects

    这是因为您在 fixture.whenStable().then(() => {....}) 内部有异步运行的断言。

    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 构造没有运气。 fakeAsyc 是唯一一个救了我的培根。
    • 对我来说 fakeAsync 也不起作用,
    猜你喜欢
    • 2020-05-07
    • 1970-01-01
    • 1970-01-01
    • 2020-02-06
    • 1970-01-01
    • 1970-01-01
    • 2017-02-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多