# Fake jasmine SpyOn before running constructor

**URL:** <https://forum.ionicframework.com/t/fake-jasmine-spyon-before-running-constructor/53052>\
**Category:** ionic-v3\
**Created:** [May 26, 2016, 10:27am UTC](https://forum.ionicframework.com/t/fake-jasmine-spyon-before-running-constructor/53052 "2016-05-26T10:27:48Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![mbakker1996](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/mbakker1996/32/96698_2.png) [@mbakker1996](https://forum.ionicframework.com/u/mbakker1996)\
**Post date:** [May 26, 2016, 10:27am UTC](https://forum.ionicframework.com/t/fake-jasmine-spyon-before-running-constructor/53052/1 "2016-05-26T10:27:48Z")

</div>

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