如何修复错误:断言错误:应该在更新模式下运行[预期=>false==true<=实际]

我正在用 ChangeDetectorRef 的角度编写代码

该功能本身运行良好。

getVersionInfos() {
concat(
  of(
    this.getApiSubs = this.aboutInfoService.getApiVersion().subscribe((data) => {
      if (data) {
        this.apiData = data;
        this.applicationCopyright = data.find(dt => dt.Name === "Web API Details").Value;          }
    })
  ),
  of(
    this.getEngineSubs = this.aboutInfoService.getEngineVersion().subscribe((data) => {
      if (data) {
        this.engineData = data;
        this.engineDetails = data.find(dt => dt.itemcode === "VER").versiontext;
        this.cd.detectChanges();
      }
    })
  )
);

}

当我为它编写单元测试代码时,它一直在失败

        this.cd.detectChanges();
        this.cd.detectChanges();

这给了我这个错误

错误:断言错误:应该在更新模式下运行 [预期=> false == true <=实际]

这是规范代码块

beforeEach(async(() => {
TestBed.configureTestingModule({
  declarations: [
    AboutComponent
  ],
  imports: [
    MatBottomSheetModule,
    HttpClientTestingModule
  ],
  providers: [
    {
      provide: AboutInfoService,
      useValue: jasmine.createSpyObj("AboutInfoService", [
        "getApiVersion",
        "getEngineVersion"
      ]),
    },
    {
      provide: ChangeDetectorRef,
      useValue: jasmine.createSpyObj("ChangeDetectorRef", ["detectChanges"])
    }
  ]
})
.compileComponents();

aboutInfoService = TestBed.get(AboutInfoService);
aboutInfoService.getApiVersion.and.returnValue(of(mockApiResponse));
aboutInfoService.getEngineVersion.and.returnValue(of(mockEngineResponse));



}));

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

  it("should create", () => {
    expect(component).toBeTruthy();
  });

如果我删除代码

从函数中它将通过单元测试

回答

我得到了一切,

我已将函数 getVersionInfos 放在 ngOnInit 而不是构造函数中


回答

我在 Angular 11 中也有这个错误。问题是我正在执行:

this.cdr.detectChanges();

在构造函数内部,这是错误的。我把它移到 ngOnInit()


以上是如何修复错误:断言错误:应该在更新模式下运行[预期=&gt;false==true&lt;=实际]的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>