Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to mock ViewController.willEnter.subscribe? #190

Closed
kamok opened this issue Dec 7, 2016 · 5 comments
Closed

How to mock ViewController.willEnter.subscribe? #190

kamok opened this issue Dec 7, 2016 · 5 comments

Comments

@kamok
Copy link

kamok commented Dec 7, 2016

When testing components that utilizes the viewController, you must mock the viewController using a viewControllerMock. By the standards of this seeder project, we are placing that class in mock.ts, and importing it in test.ts.

However, I'm having trouble mocking the willEnter() method, as even when I return an Observable, as per the docks, it's giving me a TypeError: viewCtrl.willEnter.subscribe is not a function.

Has anyone successfully mock these Observables in their app?

Here's my attempt at writing a function that returns an Observable, which returns true when subscribed to it.

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

    return observable
  };
@lathonez
Copy link
Owner

lathonez commented Dec 8, 2016

Where are you importing Observable from?

http://stackoverflow.com/questions/36568388/observable-of-is-not-a-function

@masimplo
Copy link

masimplo commented Dec 8, 2016

@kamok willEnter is not a function but an Observable to which you can subscribe. In your mock you can declare it either as a property and set it to and Observable or even better as a getter.

you can just write

import { Observable } from 'rxjs/Rx';

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

This will emit a single "marble" with the value true and then complete. If you want more fine control you can return a Subject instead and feed it with values on demand.

@lathonez
Copy link
Owner

lathonez commented Dec 8, 2016

@masimplo - legend.

@lathonez lathonez closed this as completed Dec 8, 2016
@kamok
Copy link
Author

kamok commented Dec 8, 2016

@masimplo That did the trick. Thank you. Posted on stack and nobody knew what to do.

@lathonez
Copy link
Owner

#191

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants