jeudi 26 mai 2016

spyOn method in constructor with jasmine

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);
        });
    }

Aucun commentaire:

Enregistrer un commentaire