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 test a error subscription #408

Closed
sanzlidia opened this issue Feb 27, 2019 · 5 comments
Closed

How to test a error subscription #408

sanzlidia opened this issue Feb 27, 2019 · 5 comments

Comments

@sanzlidia
Copy link

sanzlidia commented Feb 27, 2019

Hi,
I want to test a method like this:

public login(token: string): Observable<boolean> {
  const result = new Subject<boolean>();
  this.service.authenticate(token).subscribe
    ((result) => {
      this.onCompletedLogin().subscribe(() => {
        result.next(true);
      });
    }, (error) => result.next(false));
  return result.asObservable();
}

I don't know how to test the error part with the lastest version of testdouble

@searls
Copy link
Member

searls commented Feb 27, 2019

Hi @sanzlidia. Heads up that you can format your code blocks in markdown "code fences" with three backticks and a language name:

```js
some code here
```

Could you try giving a more complete example that is easily runnable? I can't figure out how I'd run this (it's apparently typescript, but Node doesn't support Observable, so I don't know how to help you without making a lot of speculation

@sanzlidia
Copy link
Author

I want to test this method in typescript.

  public functionToTest(token: string): Observable<boolean> {
    const result = new Subject<boolean>();
    this.service.authenticate(token).subscribe
      ((response: boolean) =>
      {
        result.next(response);
      }, 
      (error) => result.next(false));
    return result.asObservable();

I have two paths inside the subscribe. The happy path and the error path.

When I test the happy path (inside the subscribe) I have written the test like this and it works properly.

td.when(service.authenticate('token')).thenReturn(of(true));

The problem is that I don't know how to test the error path. I mean, when the service subscribe returns an error.

Thank you so much.

@searls
Copy link
Member

searls commented Feb 27, 2019

Yes, I understand that, but that's not a runnable function. Can you please provide a runnable example?

@sanzlidia
Copy link
Author

Hello.
Sorry, but I don't have a runnable function. But the code is like this:

 
  const result_login = new Subject<boolean>();

  public functionToTest(token: string): Observable<boolean> {
    const result = new Subject<boolean>();
    this.authenticate(token).subscribe
      ((response: boolean) =>
      {
        result.next(response);
      }, 
      (error) => result.next(false));
    return result.asObservable();
  }

  private authenticate(token: string): Observable<boolean> {
      // call an api and returns an Observable<boolean>
      return this.result_login;
  }

The test that works properly is like this:

   it('User is authenticate and complete login is ok', (done: DoneFn) => {
            //Arrange
            const _mock = td.constructor(LoginCompletedService).prototype;
            td.when(_mock.authenticate(_token)).thenReturn(of(true));

            //Action
            let result: boolean;
            _loginCompletedService.loginWithToken(_token).subscribe((value) => {
                result = value;
                //Assert
                expect(true).toEqual(result);
                done();
            });
        });

@searls
Copy link
Member

searls commented May 29, 2019

I'm sorry, but without a runnable example, it'll be too time-consuming to try to infer or speculate as to what you mean or what's needed to fully test the condition you're alluding to.

I'm going to close this for now, but will reopen if you find time to create an isolated, runnable example

@searls searls closed this as completed May 29, 2019
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

2 participants