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.

testing observable catch

See original GitHub issue

Hi 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:closed
  • Created 7 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
florinncommented, Aug 6, 2016

I was able to run the tests in your repo after doing the following changes in HttpJSONService.spec.ts:

import {Http, Response, ResponseOptions} from '@angular/http';
mockDataExtractor.callBase = true;
mockErrorHandler.callBase = true;
let mockResponse: TypeMoq.Mock<Response> = TypeMoq.Mock.ofType2(Response, [ResponseOptions]);
it('calls the API to remove data', () => {
...
  mockHttp
    .setup(x => x.delete(TypeMoq.It.isAny()))
    .returns(() => Observable.of(mockResponse.object));

    // when
    httpJSONService.delete(apiUrl).subscribe();
...
});

it('logs error to console on failure', () => {
...
  // when
  httpJSONService.delete(TypeMoq.It.isAny()).subscribe();
...
});
0reactions
b091commented, Aug 1, 2016

sorry for late response, I’m also using typescript 2.

here is my simple example of this test case: repo

Read more comments on GitHub >

github_iconTop 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 >

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