jasmine-marbles next 只发出第一个值

jasmine-marbles next only emit first value

我有一些像这样的弹珠:

import { cold, getTestScheduler } from 'jasmine-marbles'
const marbles$ = cold('--x--y|', {x: false, y: true})

当我打电话时:

getTestScheduler().flush()

x 和 y 都被发射。但是,我想这样做:

it('my test', () => {
  // setup spies and other logic here
  const marbles$ = cold('--x--y|', {x: false, y: true})
  expect(foo).toBe(bar1)
  // EMIT x FROM marbles$ here
  expect(foo).toBe(bar2)
  // EMIT y FROM marbles$ here
  expect(foo).toBe(bar3)
})

这可能吗?如果是这样,我该如何实现?谢谢

我正在寻找的是 getTestScheduler().next() 类似于你在 RxJs Subject 上调用 next 的方式 - 也许它会发出弹珠中的下一个项目,或者如果下一个项目是,它不会发出任何东西'-' ... 不确定它是如何工作的,但希望你明白我所追求的要点。

好吧,jasmine-marbles 实际上提供了一个非常方便的匹配器来测试流的输出,因此您不必以某种方式手动触发调度程序:.toBeObservable。您通过将其传递给它 另一个 流来使用它,即预期的输出。

我将稍微修改您的示例以展示其用途。假设我们正在真实模块中测试从一个流到另一个流的映射,它接受一个字符串并发出一个布尔值。

// real-module.ts
import { Observable, Subject } from 'rxjs';
import { map } from 'rxjs/operators';

export const input$: Subject<string> = new Subject ();
export const output$: Observable<boolean> = input$.pipe (map (value => ({
    IWantTheTruth       : true,
    ICantHandleTheTruth : false
}[value])));
// real-module.spec.ts
import { cold } from 'jasmine-marbles';
import { input$, output$ } from './real-module';

const schedule$ = cold ('--x--y|', { x : 'IWantTheTruth', y : 'ICantHandleTheTruth' });
const expected$ = cold ('--x--y|', { x : true, y : false });

schedule$.subscribe (input$);
expect (output$).toBeObservable (expected$);

匹配器为您运行测试调度程序,并比较实际流和预期流的结果,就好像它只是比较两个普通的可迭代对象一样。如果你故意不通过测试,你可以看到这个:

expect (cold ('-x')).toBeObservable (cold ('x-'));

此失败测试的输出错误消息如下所示(为清楚起见,我添加了换行符):

Expected [
 Object({ frame: 10, notification: Notification({ kind: 'N', value: 'x', error: undefined, hasValue: true }) })
] to equal [
 Object({ frame: 0, notification: Notification({ kind: 'N', value: 'x', error: undefined, hasValue: true }) })
].

你可以看到 frame 的值是不同的,因为弹珠的时间不同。 Notification 对象显示发出的内容的详细信息。 kind 是下一个 'N',错误 'E' 或完成 'C' 之一。