Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unit testing 如何使用jasmine大理石在rxjs管道中测试timeout()_Unit Testing_Rxjs_Timeout_Jasmine Marbles - Fatal编程技术网

Unit testing 如何使用jasmine大理石在rxjs管道中测试timeout()

Unit testing 如何使用jasmine大理石在rxjs管道中测试timeout(),unit-testing,rxjs,timeout,jasmine-marbles,Unit Testing,Rxjs,Timeout,Jasmine Marbles,我已经编写了一个管道来过滤可观察的输入。在管道中,我使用timeout()操作符指定了一个超时,如果源没有及时发出预期值,它将中止等待。 我想用茉莉花弹珠测试超时情况,但我无法让它工作。 我相信expect(source).tobeobbservable()在源代码发出之前进行计算 看 要测试的管道: source=cold('a',{a:{id:'a'}).pipe( 超时(500), 过滤器((a)=>false), catchError((err)=>{ 返回({timeout:true}

我已经编写了一个管道来过滤可观察的输入。在管道中,我使用timeout()操作符指定了一个超时,如果源没有及时发出预期值,它将中止等待。 我想用茉莉花弹珠测试超时情况,但我无法让它工作。 我相信
expect(source).tobeobbservable()
在源代码发出之前进行计算

要测试的管道:

source=cold('a',{a:{id:'a'}).pipe(
超时(500),
过滤器((a)=>false),
catchError((err)=>{
返回({timeout:true})
}),
采取(1)
);
使用toPromise()进行测试的效果与预期一致:

expect(wait source.toPromise()).toEqual({timeout:true});
茉莉花大理石测试

const expected=cold('500ms(a |)),{a:{timeout:true});
expect(源)。tobeobbservable(预期);
由于错误而失败

Expected $.length = 0 to equal 2.
Expected $[0] = undefined to equal Object({ frame: 500, notification: Notification({ kind: 'N', value: Object({ timeout: true }), error: undefined, hasValue: true }) }).
Expected $[1] = undefined to equal Object({ frame: 500, notification: Notification({ kind: 'C', value: undefined, error: undefined, hasValue: false }) }).

jasmine marbles 0.5.0最近增加了对时间进程的支持()。软件包中添加了其他测试规范,演示了实现所需功能的几种可能方法之一。以下是我使用Stackblitz示例收集到的一些选项

选择1 当您在测试方法之外初始化可观察到的源时(例如,在
beforeach
),您必须明确初始化并将测试计划传递到
timeout
,以使
expect().toBeObservable()
工作。但是,请注意,此更改将打破“应与toPromise一起工作”测试。(我不知道为什么,但是
toPromise()
似乎不适合这种方法。)

description('Marble testing with timeout',()=>{
让源;
在每个之前(()=>{
//必须在“beforeach”中显式初始化测试计划程序。
initTestScheduler()
source=cold('a',{a:{id:'a'})。管道(
//您必须明确通过测试计划程序。
超时(500,getTestScheduler()),
过滤器((a)=>false),
catchError(err=>{
返回({timeout:true})
}),
采取(1)
);
});
它('应该与toBeObservable一起工作',()=>{
const expected=cold('500ms(a |)),{a:{timeout:true});
expect(源)。tobeobbservable(预期);
});
});
选择2 您可以稍微重构一些东西,并在测试方法中初始化可观察到的源代码(而不是在每个之前的
中)。您不需要显式地初始化测试调度程序(jasmine marbles将在测试方法运行之前为您进行初始化),但是您仍然需要将它传递到
timeout
。注意
createSource
函数如何与测试计划程序或默认计划程序一起使用(如果
scheduler
参数未定义)。此选项适用于“应与toPromise配合使用”测试和“应与toBeObservable配合使用”测试

description('Marble testing with timeout',()=>{
const createSource=(调度程序=未定义)=>{
return cold('a',{a:{id:'a'}).pipe(
//必须显式通过测试计划程序(或未定义以使用默认计划程序)。
超时(500,调度程序),
过滤器((a)=>false),
catchError(err=>{
返回({timeout:true})
}),
采取(1)
);
};
它('应该与toPromise一起使用',异步()=>{
const source=createSource();
expect(wait source.toPromise()).toEqual({timeout:true});
});
它('应该与toBeObservable一起工作',()=>{
const source=createSource(getTestScheduler());
const expected=cold('500ms(a |)),{a:{timeout:true});
expect(源)。tobeobbservable(预期);
});
});
选择3 最后,如果显式使用测试调度器的
run
方法,可以跳过将测试调度器传递到
timeout
,但必须使用
expectObservable
(与
expect().toBeObservable()
相反)。它工作正常,但Jasmine会报告警告“SPEC没有期望”

description('Marble testing with timeout',()=>{
让源;
在每个之前(()=>{
source=cold('a',{a:{id:'a'})。管道(
超时(500),
过滤器((a)=>false),
catchError(err=>{
返回({timeout:true})
}),
采取(1)
);
});
它('应该与调度程序和expectObservable一起工作',()=>{
const scheduler=getTestScheduler();
scheduler.run(({expectObservable})=>{
expectObservable(source.toBe('500ms(0 |)),[{timeout:true}]);
});
});
});

看起来时间进程仍在被烧制成
茉莉花大理石
。我想我应该插入
rxjs大理石
。使用它的良好体验。它支持茉莉花(以及其他),并且他们有大量不同用例的示例。谢谢@seniorquico,反应很好!与Stackblitz示例相反,我无法让选项1在我的生产代码中工作,我不知道为什么。但无论如何,我更喜欢选项3,因为它不涉及在生产代码中处理调度程序。旁注:我避免了带expect()的茉莉花警告。无()