我有一些像这样的弹珠:
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)
})这个是可能的吗?如果是这样的话,我如何做到这一点?Thx
我要找的是类似于getTestScheduler().next()的东西,类似于在RxJ主题上调用next --也许它会发出弹珠中的下一个项目,或者如果下一个项目是'-‘,它就不会发射任何东西。不太确定它会如何运作,但希望你能明白我想要什么。
发布于 2019-08-01 12:24:22
好吧,茉莉弹珠实际上为测试流的输出提供了一个非常方便的匹配器,所以您不必以某种方式手动触发调度程序:.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$);matcher为您运行测试调度程序,并比较实际流和预期流的结果,就好像它只是比较两个普通的迭代程序一样。如果您故意不通过测试,您可以看到这一点:
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是next的'N'之一,'E'是错误的,'C'是完整的。
https://stackoverflow.com/questions/56990192
复制相似问题