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

6

45

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

Executor answered 14/1, 2021 at 1:21 Comment(2)
Do you call the getVersionInfos function inside the component's constructor?Tran
thank you, i changed the location of the function and placed it on ngOnInit @shalomExecutor
E
74

I got it all working,

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

Executor answered 15/1, 2021 at 6:26 Comment(0)
M
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()
Moue answered 28/3, 2021 at 16:56 Comment(0)
F
18

Avoid keeping this._changeDetectorRef.detectChanges() method call in the constructor or in the method which will get called from the constructor. Angular will throw this error in both cases.

Fink answered 19/8, 2021 at 14:0 Comment(1)
any explanation why?Hearken
H
1

I Noticed this issue too, this issue is mostly because of conflict in angular cli version, i had the angular cli version of 11.1.2, i have downgraded to 11.0.7 and it worked for me.

In my case, this issue was causing the UI to not to load properly.

As i saw other answers, moving the functions or code from Constructor to ngOnInit is not the good option, it just the matter of cli version compatibility.

Hueston answered 24/8, 2023 at 10:36 Comment(0)
J
0

Make sure to include any custom components used in your component. Just include the child component name in declarations in your .spec.ts (along with an import of course).

Jameejamel answered 9/12, 2022 at 21:27 Comment(0)
L
0

If you have a project with module federation, check angular packages version in the host app and connected apps, they must match exactly. After editing versions, remove node_modules and package-lock.json and install them again.

Lenz answered 15/3, 2024 at 13:52 Comment(1)
Why those versions must match and how does this behaviour generates the error?Pomiferous

© 2022 - 2025 — McMap. All rights reserved.