Fake jasmine SpyOn before running constructor


#1

Hi,
I want to spyOn a promise and fake that promise in my unit test but the problem is that if I run first the contructor that the problem that he first run the promise and then run the Spyon.

But when i first run the spyOn and then the constructor it gives a error that storage is undefined.

Does someone know how to fix this?

Spec file:

describe(‘Settings Service’, () => {

beforeEach(() => {
    settingsService = new SettingsService(); // This gives a error beceause it runs the promise 
    spyOn(settingsService.storage, 'get').and.callFake((key: String): Promise<string> => {
        return new Promise((resolve, reject) => { resolve('url'); });
    });
});

constructor:

constructor() {
        this.storage = new Storage(LocalStorage);
        this.storage.get('url').then(data => {
            this.setLink(data);
        });
}

I also tried this without any success:

beforeEach(() => {
        let injector: any = ReflectiveInjector.resolveAndCreate([SettingsService]);
        settingsService = injector.get(SettingsService);

        spyOn(settingsService.storage, 'get').and.callFake((key: String): Promise<string> => {
            return new Promise((resolve, reject) => { resolve('https://secure.info/pascal'); });
        });
    });