测试在角度的管道内的地图(隔离测试)
我有一种带有图像的管道的方法。当我运行测试时,我找不到进入该地图的方法。
这是我的组件代码
public getParents(): void {
Eif (this.filter.siteId !== null) {
this.parents$ = this.parentsM().pipe(
map(items => items.data),
catchError(error => {
this.messageService.errorHandler(error);
return Array<any>();
}));
}
}
it("getParents with siteId", async(() => {
const mockItems:Response = {
codError: "",
msgError: "",
data: "test"}
let spy = spyOn<any>(component, "parentsM").and.returnValue(scheduled([mockItems], asyncScheduler));
component.getParents();
component.parentsM().subscribe(items => {
expect(items).toEqual(mockItems);
});
spy.calls.reset();
}));
这是我的规格。
。 alt =“管道地图测试”>
I have a method that has a pipe with a map inside of it. When I run the test I can't find a way to get into that map.
This is my component code
public getParents(): void {
Eif (this.filter.siteId !== null) {
this.parents$ = this.parentsM().pipe(
map(items => items.data),
catchError(error => {
this.messageService.errorHandler(error);
return Array<any>();
}));
}
}
This is my Spec.ts code
it("getParents with siteId", async(() => {
const mockItems:Response = {
codError: "",
msgError: "",
data: "test"}
let spy = spyOn<any>(component, "parentsM").and.returnValue(scheduled([mockItems], asyncScheduler));
component.getParents();
component.parentsM().subscribe(items => {
expect(items).toEqual(mockItems);
});
spy.calls.reset();
}));
This is my code coverage
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里有多个问题。实际上,您必须订阅
this.parents $
才能执行地图,这使您的测试异步。在您的测试中,您正在订阅parentsm()
的结果,我猜这不会返回this.parents $
,因此您不订阅你写的烟斗。您也可以使用Waitforasync
而不是async
。请尝试以下操作:
Multiple issues here. You actually have to subscribe to
this.parents$
to execute the map, which makes your test async. In your test, you're subscribing on the result ofparentsM()
, which I guess doesn't return exactlythis.parents$
, so you're not subscribing to the pipe you wrote. You may also usewaitForAsync
instead ofasync
.Please try this: