茉莉花大理石下一个仅发出第一值

时间:2019-07-11 13:19:18

标签: angular jasmine jasmine-marbles

我有一些像这样的弹珠:

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主题上的下一个呼叫方式-也许它将发射弹珠中的下一个项目,或者如果下一个项目是'- '...不确定是否会有效,但希望您了解我所追求的目标。

1 个答案:

答案 0 :(得分:1)

嗯,茉莉花大理石实际上为测试流的输出提供了一个非常方便的匹配器,因此您不必以某种方式手动触发调度程序:.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'之一。