testing observable catch
See original GitHub issueHi probably it’s not issue of typemoq but maybe you can help me, and it might be a good example for this lib.
I cannot find a way to test rxjs catch…
Test code:
let mockHttp:TypeMoq.Mock<Http>;
let mockErrorHandler:TypeMoq.Mock<ErrorHandler>;
let httpJSONService:HttpJSONService;
beforeEach(() => {
mockHttp = TypeMoq.Mock.ofType(Http);
mockErrorHandler = TypeMoq.Mock.ofType(ErrorHandler);
httpJSONService = new HttpJSONService(mockHttp.object, mockErrorHandler.object);
});
it('logs error to console on failure', () => {
const error:any = 'sample-error';
mockHttp
.setup(x => x.delete(TypeMoq.It.isAny()))
.returns(x => Observable.throw(new Error(error))); // does not throw an error, does not trigger catch
// .throws(new Error(error)); // <-- throws error, does not trigger catch
httpJSONService.delete(TypeMoq.It.isAny());
mockErrorHandler.verify(x => x.logToConsole(error), TypeMoq.Times.once());
});
Code:
constructor(private http:Http,
private errorHandler:ErrorHandler) {
}
public delete<T>(url:string):Observable<T> {
return this.http.delete(url)
.catch((err) => this.errorHandler.logToConsole<T>(err));
}
Issue Analytics
- State:
- Created 7 years ago
- Comments:7 (4 by maintainers)
Top Results From Across the Web
Testing error case with observables in services - Stack Overflow
What I usually do for most of my observable services, is to create a mock whose methods just returns itself. The mock service...
Read more >Testing Observables in Angular - Netanel Basal
RxJS marble testing is an excellent way to test both simple and complex observable scenarios. Marble testing uses a similar marble language to...
Read more >How to test Observables. The ultimate guide - Medium
In this blog post, we are going to see the two different strategies on how to test an Observable. The “subscribe and assert...
Read more >RxJS Testing — Write Unit Tests for Observables | by Denis Loh
The test simply finished before the emitted value has been evaluated. A simple approach would be to make an asynchronous test out of...
Read more >RxJs testing patterns - Technical Blog at Generic UI Angular ...
How to check whether the value is not emitted from the observable? The most popular solution is to use the done function provided...
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
I was able to run the tests in your repo after doing the following changes in HttpJSONService.spec.ts:
sorry for late response, I’m also using typescript 2.
here is my simple example of this test case: repo