How to mock ViewController.willEnter.subscribe?
See original GitHub issueWhen 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:
- Created 7 years ago
- Comments:5 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@masimplo That did the trick. Thank you. Posted on stack and nobody knew what to do.
@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
This will emit a single “marble” with the value
true
and then complete. If you want more fine control you can return aSubject
instead and feed it with values on demand.