![](/img/trans.png)
[英]How to nicely throw an error to an Angular RXJS Observable subscriber
[英]rxjs how to expect an observable to throw error
在我的 TypeScript 應用程序中,我有一個返回 rxjs Observable 的方法,在某些情況下,它可以返回throwError :
import { throwError } from 'rxjs';
// ...
getSomeData(inputValue): Observable<string> {
if (!inputValue) {
return throwError('Missing inputValue!');
}
// ...
}
我如何編寫測試來涵蓋這個特定案例?
您可以使用 RxJS Marble 圖表測試來測試它。 就是這樣:
const getSomeData = (inputValue: string): Observable<string> => {
if (!inputValue) {
return throwError('Missing inputValue!');
}
// e.g.
return of(inputValue);
};
describe('Error test', () => {
let scheduler: TestScheduler;
beforeEach(() => {
scheduler = new TestScheduler((actual, expected) => {
expect(actual).toEqual(expected);
});
});
it('should throw an error if an invalid value has been sent', () => {
scheduler.run(({ expectObservable }) => {
const expectedMarbles = '#'; // # indicates an error terminal event
const result$ = getSomeData(''); // an empty string is falsy
expectObservable(result$).toBe(expectedMarbles, null, 'Missing inputValue!');
});
});
it('should emit an inputValue and immediately complete', () => {
scheduler.run(({ expectObservable }) => {
const expectedMarbles = '(a|)';
const result$ = getSomeData('Some valid string');
expectObservable(result$).toBe(expectedMarbles, { a: 'Some valid string' });
});
});
});
有關如何編寫這些測試的更多信息,請查看此鏈接。
我想你的完整案例類似於這樣的東西
// first there is something that emits an Observable
export function doSomethingThatReturnsAnObservable() {
return createSomehowFirstObservable()
.pipe(
// then you take the data emitted by the first Observable
// and try to do something else which will emit another Observable
// therefore you have to use an operator like concatMap or switchMap
// this something else is where your error condition can occur
// and it is where we use your getSomeData() function
switchMap(inputValue => getSomeData(inputValue))
);
}
}
// eventually, somewhere else, you subscribe
doSomethingThatReturnsAnObservable()
.subscribe(
data => doStuff(data),
error => handleError(error),
() => doSomethingWhenCompleted()
)
錯誤條件的測試可能看起來像這樣
it('test error condition'), done => {
// create the context so that the call to the code generates an error condition
.....
doSomethingThatReturnsAnObservable()
.subscribe(
null, // you are not interested in the case something is emitted
error => {
expect(error).to.equal(....);
done();
},
() => {
// this code should not be executed since an error condition is expected
done('Error, the Observable is expected to error and not complete');
}
)
})
除了拉戈曼的回答。 您可以簡化獲取 testScheduler 的方法。
describe('Error test', () => {
it('should throw an error if an invalid value has been sent', () => {
getTestScheduler().run(({ expectObservable }) => { // getTestScheduler from jasmine-marbles package
const expectedMarbles = '#'; // # indicates an error terminal event
const result$ = getSomeData(''); // an empty string is falsy
expectObservable(result$).toBe(expectedMarbles, null, 'Missing inputValue!');
});
});
it('should emit an inputValue and immediately complete', () => {
getTestScheduler().run(({ expectObservable }) => {
const expectedMarbles = '(a|)';
const result$ = getSomeData('Some valid string');
expectObservable(result$).toBe(expectedMarbles, { a: 'Some valid string' });
});
});
});
另一種方法是檢查操作員是否顯示錯誤,如下所示對其進行管道傳輸。 我假設您正在使用 Chai。
import { catchError } from 'rxjs/operators';
it('must throw an error', done => {
getSomeData().pipe(catchError((e) => [e])).subscribe(e => {
expect(e).to.be.an.instanceof(Error);
done();
})
})
Source 如何測試拋出錯誤的 RxJS 操作
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.