Question

how to fix Error: ASSERTION ERROR: Should be run in update mode [Expected=> false == true <=Actual]

Im writing a code with in angular with ChangeDetectorRef

The function itself is working fine.

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

}

When i wrote the Unit Test code for it keeps on failing on the

        this.cd.detectChanges();

which gives me this error

Error: ASSERTION ERROR: Should be run in update mode [Expected=> false == true <=Actual]

and this is the spec code block

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

if i remove the code

        this.cd.detectChanges();

from the function it will pass the unit test

 46  43466  46
1 Jan 1970

Solution

 74

I got it all working,

I have placed the function getVersionInfos in the ngOnInit instead of inside the constructor

2021-01-15

Solution

 36

I've this error too in Angular 11. The problem was that I was executing:

this.cdr.detectChanges();
Inside the constructor, that is wrong. I moved It to ngOnInit()

2021-03-28