I'm facing an issue with my angular.io tests using jasmine (v2.99).
I've set up a spyObject for an Angular service and mocked some functions, which worked as expected. Now, I need to call an original function (non-mock) in the same service. This particular function returns a promise.
I have discovered that this can be achieved with callThrough() in jasmine.
So, I implemented it accordingly and it seems to work partially ;)
describe('Component2Test', () => {
let component: Component2Test;
let fixture: ComponentFixture<Component2Test>;
let dataServiceSpy: jasmine.SpyObj<DataService>;
beforeEach(() => {
const dataSrvSpy = jasmine.createSpyObj('DataService', ['getUserObject',
'getLocalStorageObject', 'getAllLocalStorageKeys', 'setLocalStorageObject']);
dataSrvSpy.getUserObject.and.returnValue(tua);
dataSrvSpy.getAllLocalStorageKeys.and.returnValue(Promise.resolve(localStorageData));
dataSrvSpy.setLocalStorageObject.and.callThrough();
}
TestBed.configureTestingModule({
imports: [],
declarations: [Component2Test],
providers: [
{provide: DataService, useValue: dataSrvSpy},
]
});
dataServiceSpy = TestBed.get(DataService);
fixture = TestBed.createComponent(Component2Test);
component = fixture.componentInstance;
});
test
fit('should set data in localStorage', fakeAsync(() => {
dataServiceSpy.setLocalStorageObject(foundData[foundDataKeys[1]], foundDataKeys[1]).then();
});
Even though the original function is called and the data is written as expected, Jasmine throws the following error message:
TypeError: Cannot read property 'then' of undefined
Original function in service
setLocalStorageObject(o: object, key: string): Promise<boolean>{
return new Promise((resolve, reject) => {
this._lclStrgSrv4User.storeJsonDataInLocalStorage(o, key).then((resp) => {
resolve(resp);
}, (err) => {
reject(err)
});
})
}
Therefore, my question is:
How can I define the callThrough() correctly?
Thanks in advance, Tom