question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to mock ViewController.willEnter.subscribe?

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
kamokcommented, Dec 8, 2016

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

1reaction
masimplocommented, 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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ionic2 - How to mock ViewController.willEnter in Ionic 2? - Stack ...
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...
Read more >
Unable to use ViewController in unit testing with or without ...
Injecting and mocking out ViewController for unit tests causes two separate issues. The first error being Failed: Can't resolve all parameters ...
Read more >
Unit Testing View Controllers and Views in Swift
Learn how to unit test UIViewController and UIView in Swift 5 with Xcode and XCTest. We'll cover: benefits of unit testing view controllers; ......
Read more >
Unit Testing Model View Controller on iOS with Swift | by Ali Aga
Inject the MockView in the View Controller. Now let's make the unit test functional by injecting the mock view and an AccountModel in...
Read more >
Ways to Load UIViewController in a Unit Test
This is a type of UIViewController that creates views programmatically. If you are interested in video lessons on how to write Unit tests...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found