Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 305
nhoyti Avatar asked Jan 14 '21 01:01

nhoyti


2 Answers

I got it all working,

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

like image 83
nhoyti Avatar answered Nov 17 '22 06:11

nhoyti


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()
like image 20
Martin Muñoz Avatar answered Nov 17 '22 05:11

Martin Muñoz