【发布时间】:2022-01-13 21:59:10
【问题描述】:
总结:如何在 Jasmine 超时的情况下测试 rxjs ThrowError?
我正在测试一项可以返回已完成的 Observable 或错误的服务。出于测试目的,我们可以用这个服务来表示它:
import { Observable, of, throwError } from 'rxjs';
export class MyService {
foo(shouldError: boolean): Observable<any> {
if (shouldError) {
return throwError('');
} else {
return of();
}
}
}
为了测试这段代码,我做了以下测试:
describe('MyService', () => {
let service: MyService;
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(MyService);
});
it('handles observable', (done) => {
const shouldError = false;
service.foo(shouldError).subscribe(
(_) => done(),
(_) => done.fail()
);
});
it('handles error', (done) => {
const shouldError = true;
service.foo(shouldError).subscribe(
(_) => done.fail(),
(_) => done()
);
});
}
但是,这段代码最终导致 Jasmine 超时:
Error: Timeout - Async function did not complete within 5000ms (set by jasmine.DEFAULT_TIMEOUT_INTERVAL)
我做错了什么?
【问题讨论】:
标签: typescript rxjs jasmine