我有一个可观察的source
和一个将f
元素转换为source
的函数Promise
。我想将该函数应用于source
并使用chai:
import { TestScheduler } from 'rxjs/testing/TestScheduler';
import 'rxjs/add/operator/mergeAll';
// expect is from chai
const marbles = new TestScheduler((a, b) => expect(a).to.deep.equal(b));
const source = marbles.createColdObservable('a-a-a-a');
const map = source.mergeMap(async a => a);
marbles.expectObservable(map).toBe('a-a-a-a');
marbles.flush();
预期结果是测试通过。实际结果是:
AssertionError: expected [] to deeply equal [ Array(4) ]
为什么观察结果为空?
答案 0 :(得分:2)
我遇到了同样的问题,事实证明承诺无法如您所愿进行测试。
有关详情,请参见此页 https://rxjs-dev.firebaseapp.com/guide/testing/marble-testing
总结:
已知问题
您不能直接测试使用Promise或使用任何其他调度程序(例如AsapScheduler)的RxJS代码
// Some RxJS code that also consumes a Promise, so TestScheduler won't be able
// to correctly virtualize and the test will always be really async
const myAsyncCode = () => from(Promise.resolve('something'));
it('has async code', done => {
myAsyncCode().subscribe(d => {
assertEqual(d, 'something');
done();
});
});