2

I use viewCtrl.willEnter to generate a Observable that I call .subscribe on in a Component. Currently, the tests are throwing a viewCtrl.willEnter.subscribe is not a valid function error. This is because in Ionic 2, I have to mock the viewController, and I have not successfully done so for this particular method.

This is what I got so far.

public willEnter(): Observable<boolean> {
    let observable = Observable.create(function (observer) {
    observer.onNext(true);
    observer.onCompleted();
    });

    return observable
  };
Ka Mok
  • 1,937
  • 3
  • 22
  • 47
  • what you are trying to really achive – Mohan Gopi Dec 07 '16 at 19:54
  • @MohanGopi mock the Ionic 2 API function willEnter() in the View Controller. I'm using Jasmine and Karma, and initializing the test suite with Ionic's TestController throws an error. Hence, by mocking that function, it will allow me the unit test the component. – Ka Mok Dec 07 '16 at 20:09
  • why not put the logic of `willEnter()` in a different method and return/test that? If it doesn't get called you can do so by looking at this S.O. answer http://stackoverflow.com/questions/37408801/testing-ngonchanges-lifecycle-hook-in-angular-2 – Ivar Reukers Dec 07 '16 at 20:31
  • @Ivaro18 I'm not quite sure I know what you mean, updating my question to be more clear. – Ka Mok Dec 08 '16 at 14:20

1 Answers1

1

I got a solution. It was pretty easy, I just don't quite know the syntax for RXJS Observable.

import { Observable } from 'rxjs/Rx';

public get willEnter(): Observable<boolean>{
   return Observable.of(true);
}

Place this in mock.ts and just import it into your test suite.

I'm using lathonez's starter app with Clicker, where the test is configured for unit and e2e.

Ka Mok
  • 1,937
  • 3
  • 22
  • 47