简体   繁体   English

从 Angular 4 升级到 6 后,fixture.whenStable() 停止工作/调用

[英]fixture.whenStable() is stopped working/called after upgrading from Angular 4 to 6

So I had few test cases which were checking some attributes of components (such as button).所以我很少有测试用例来检查组件的某些属性(例如按钮)。

Earlier I had test cases which used to working something like:早些时候,我有测试用例,用于工作如下:

            fixture.whenStable().then(()=>{
                const nextBtn = fixture.debugElement.nativeElement.querySelector(
                    '#create-btn'
                );
                console.log(nextBtn)
                expect(nextBtn.getAttribute('ng-reflect-disabled')).toBe('true');;
            })

suddenly, the code is not going inside fixture.whenStable().then(() => {}) , the test cases are passing with a warning such as突然,代码没有进入fixture.whenStable().then(() => {}) ,测试用例通过警告,如

SomeComponent should have button disabled has no expectations SomeComponent 应该禁用按钮没有期望

I cant find anything over internet.我在互联网上找不到任何东西。 Has anyone faced similar issue.有没有人遇到过类似的问题。

I also tried wrapping it around async() of it but no luck我也尝试将它包裹在它的async()周围it但没有运气

Try to add async to beforeEach:尝试将异步添加到 beforeEach:

beforeEach(async(() => { // <-- use async
    TestBed.configureTestingModule({
        declarations: [AppComponent],
    });

    fixture = TestBed.createComponent(AppComponent);

    component = fixture.componentInstance;

    fixture.detectChanges();
}));

You can try following things.您可以尝试以下操作。

it('TEST', async(() => {
    fixture.whenStable().then(() => {
        fixture.detectChanges(); // you can write this
        const nextBtn = fixture.debugElement.nativeElement.querySelector('#create-btn');
        expect(nextBtn.getAttribute('ng-reflect-disabled')).toBe('true');
    });
}));

for angular 11 you can wrap beforeEach's callback into waitForAsync if you want to test somesing like async ngOnInit对于 angular 11,如果您想测试像async ngOnInit这样的async ngOnInit您可以将 beforeEach 的回调包装到waitForAsync 中

 beforeEach(
    waitForAsync(() => {
      fixture = TestBed.createComponent(SomeFeatureComponent);
      component = fixture.componentInstance;
      fixture.detectChanges();
    })
  );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 角度4.3.0在执行异步函数后调用fixture.whenStable - angular 4.3.0 calling fixture.whenStable after executing an asynchronous function 使用Angular夹具。当同步稳定 - Using Angular fixture.whenStable synchronously Angular Karma:从 Angular v8 升级到 v12 后,await fixture.whenStable 总是超时 - Angular Karma: await fixture.whenStable always times out after upgrade from Angular v8 to v12 Angular 中 async/await 和 async/fixture.whenStable 的区别 - Difference between async/await and async/fixture.whenStable in Angular 如何在Angular 2测试中对fixture.whenStable使用wait - How to use await on fixture.whenStable in Angular 2 Tests 如果不在异步测试执行区域内,fixture.whenStable() 实际上是否在我的角度测试中执行任何操作? - Does fixture.whenStable() actually do anything in my angular tests if not within an async test execution zone? 测试我的组件时,夹具.whenStable()永远无法解析 - fixture.whenStable() never resolves when testing my component Angular2 whenStable()不能与observable一起使用? - Angular2 whenStable() not working with observable? 从Angular 5升级到6后,过滤不起作用 - Filtering is not working after upgrading from Angular 5 to 6 mat-contenteditable 在将 angular、angular mat 升级到版本 13 后停止工作 - mat-contenteditable stopped working after upgrading angular, angular mat to version 13
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM