1

我有一些这样的弹珠:

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 主题上调用 next 的方式 - 也许它会在弹珠中发出下一个项目,或者如果下一个项目是“-”,它不会发出任何内容......不完全确定如何它会起作用,但希望你能明白我所追求的要点。

4

1 回答 1

1

好吧,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'for next、'E'for error 或'C'for complete 之一。

于 2019-08-01T12:24:22.893 回答