-
Notifications
You must be signed in to change notification settings - Fork 142
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
Comments
Hi @sanzlidia. Heads up that you can format your code blocks in markdown "code fences" with three backticks and a language name:
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 |
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. |
Yes, I understand that, but that's not a runnable function. Can you please provide a runnable example? |
Hello.
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();
});
}); |
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 |
Hi,
I want to test a method like this:
I don't know how to test the error part with the lastest version of testdouble
The text was updated successfully, but these errors were encountered: