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
I got it all working,
I have placed the function getVersionInfos in the ngOnInit instead of inside the constructor
I've this error too in Angular 11. The problem was that I was executing:
this.cdr.detectChanges();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With