Hi I have a function which I would like to unit test. This is my function.
let docProcess: IDocProcess;
public onCalling(args) {
if (this.docProcess.viewMode) {
this.makeCalls(args);
}
}
This IDocProcess is an interface:
export interface IDocProcess {
viewMode: boolean;
editMode: boolean;
deleteMode: boolean;
}
I would like to unit test the above function. If I pass docProcess.viewMode as true, makeCalls(args) function should be called. If false, then the function must not be called.
The following is my Unit test code:
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [RouterTestingModule],
declarations: [CallsComponent],
providers: [
MockData
],
schemas: [CUSTOM_ELEMENTS_SCHEMA]
}).compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(CallsComponent);
component = fixture.componentInstance;
});
it('should call makeCalls() only on View Mode', () => {
component.docProcess.viewMode = true;
fixture.detectChanges();
component.onCalling(mockArgsData);
expect(component.makeCalls).toHaveBeenCalled();
But the test results get failed. Getting cannot read properties of undefined (reading:'viewMode').
Is this because the Testing module couldn't recognize the interface. A similar situation like this, I am also getting "Cannot set properties of undefined (setting: variable_name)"
Please help me in resolving this.