38

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

Lin Du
  • 88,126
  • 95
  • 281
  • 483
nhoyti
  • 1,615
  • 2
  • 26
  • 42

5 Answers5

59

I got it all working,

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

nhoyti
  • 1,615
  • 2
  • 26
  • 42
31

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()
Martin Muñoz
  • 436
  • 4
  • 6
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.

Sumit Tawal
  • 379
  • 1
  • 4
  • 9
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).

Vahe Aslanyan
  • 76
  • 1
  • 4
0

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.

starball
  • 20,030
  • 7
  • 43
  • 238